function can be called from ## operator

N

nik

#include <stdio.h>
#define paster( n ) printf( "token" #n " = %d", token##n##() )
int token9();

int main()
{
paster(9);
}
int token()
{
return (9)
}
does it work
if no then tell me how it works with example
 
G

gMorphus

did you try it and it didn't work?

well, it works (except you declared the function as token, not token9).

next time, don't post questions like - "does it work?"
try it yourself first, and if it doesn't ask what is the problem or
what can you do.

P.s.
It is ugly...
 
K

Kaz Kylheku

nik said:
#include <stdio.h>
#define paster( n ) printf( "token" #n " = %d", token##n##() )

Why are you pasting the N and the ( together into one token???
int token9();

int main()
{
paster(9);

This tries to create the replacement token sequence

printf("token" "9" = %d", token9())

where the "token9(" is all one token! However "token9(" doesn't have
the syntax of a valid token. If the ## operator is used to create an
invalid token, the behavior is undefined.

"token9" is a valid token; "(" and ")" are also valid tokens by
themselves.

All you want is

token ## n ()

You just want to glue the result of expanding the parameter n with the
"token" prefix. Leave the function call operator out of it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top