Program below runs fine on windows. But compilation error occurs on linux "error: pasting "." and "config" does not give a valid preprocessing token"
Any reason??? i cant understand why....
#include <stdio.h>
typedef struct pr {
int config;
}pr_t;
#define JOIN(x,y) x.##y
void main()
{
pr_t temp = {5};
printf("Value %d\n", JOIN(temp, config)); //temp.config
return 0;
}
开发者_JS百科
Try without the ## :)
#define JOIN(x,y) x.y
The macro concatenation operator, ##, should only be used between two macro parameters. You have a period between them which serves to delimit the two parameter names. So as ykatchou suggested, just edit out the ## operator from the macro definition:
#define JOIN(x,y) x.y
which should still work fine in your windows compiler.
Two tokens that don't together form a valid token cannot be pasted together using ##
.
The compiler error says that clearly:
error: pasting "." and "config" does not give a valid preprocessing token
As suggested by others you can drop ##
altogether.
More info here.
It's to do with trying to paste a literal string and a token together. This behaviour was changed in gcc 2.7 onwards, see for example the info here: http://weblog.pell.portland.or.us/~orc/2004/12/30/000/index.html
You should be able to remove the ## and simply concatenate the operators:
#define JOIN(x,y) x.y
精彩评论