Macros

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Locked
ss_teven
DC Developer
DC Developer
Posts: 12
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Oct 17, 2001 7:44 pm
Location: Perth, Western Australia
Has thanked: 0
Been thanked: 0
Contact:

Macros

Post by ss_teven »

this is non-dreamcast related, but this forum seems a little dead ~_~.. so, i just want a little help with macros... (sorry if i sound like a dumbass in the process.. ;) hehe

say i got this code..

Code: Select all

typedef struct {
  void (*blah)(void);
}t_blah;
t_blah *blah;

void init_tester();

#define tester 
#define init(NAME) init_##NAME

void main() {
        blah->blah = init(tester);
}
as you can see blah->blah just calls init_tester
is there a way that i can get it to do the value of NAME instead of the actual name, so u can have something like this,...

Code: Select all

typedef struct {
  void (*blah)(void);
} t_blah;
t_blah *blah;

void init_hello();
void init_temp();

void main() {
        char temp[0x05];
        strcpy(temp,"hello");
        blah->blah = init(temp);
}
as u can see right now using
#define init(NAME) init_##NAME
will call init_temp(); , but i want it to call
init_hello();..... i've tried init("hello"), but that
just returns 0 :(

any help folks ?

thanks in advanced....

- Steven
Heliophobe_
Smeg Creator
Smeg Creator
Posts: 170
Joined: Wed Oct 17, 2001 7:44 pm
Has thanked: 0
Been thanked: 0

Post by Heliophobe_ »

What you're trying to do is use a value determined at runtime (the contents of string "temp") in computation that is made at (actually before) compile time, which is when #define macros are processed. Can't be done.

In fact, there's no guarantee that the names of functions will be preserved in any manner after the files are linked into an executable. It generally won't be, and if it is (say, if debugging information is included in the build) there's no C standard way that it is done, and no sane method of getting at a table of names and function addresses.

I'd recommend a change of design so that knowing function names isn't necessary, but if you must have this functionality, you'll have to make up a table of function addresses and names - create a structure type consisting of a string and a t_blah function pointer, and make an array of those structures, so that you can scan it to find a pointer to the function.
ss_teven
DC Developer
DC Developer
Posts: 12
Joined: Wed Oct 17, 2001 7:44 pm
Location: Perth, Western Australia
Has thanked: 0
Been thanked: 0
Contact:

Post by ss_teven »

oh okay,

yeah, i was actually thinking if i couldnt do it this way then i'd have to make up a table...

well, thanks for your help..

:)

- Steven
Locked