I have had a problem with running a program that (when simplified) looks something like this.
#include <stdio.h>
#define ADDR_TOKEN_MACRO(x) & ## x
int main() {
int *i;
int **y;
int b = 0xDEADBEEF;
*y = ADDR_TOKEN_MACRO(b);
return 0;
}
I'm trying to set the integer pointer i's address to 0xDEADBEEF, but if I wanted to have the address be inputted by the user, I have come to the conclusion that this is the best way to do so. However, I keep getting this error:
error: pasting "&" and "b" does not give a valid preprocessing token
First of all, is the idea behind the program even possible in C? And if the first question is true, can a macro like this be implemented?
Your macro is trying to take &
and b
and combine it into a single token &b
. This is not a valid identifier, so you can't do this. What you want however is simpler than that:
#define ADDR_TOKEN_MACRO(x) &x
Now you have the address-of operator applied to the given token.
Actually, using a macro doesn't make much sense here at all. You can just do this:
*y = &b;
This is a problem however because y
was not assigned a value but you try to dereference y
. I think what you're really trying to do is:
int *i = (int *)0xDEADBEEF;
But then it's also questionable if you really want to do this, because the user shouldn't need to know anything about a specific address.
User contributions licensed under CC BY-SA 3.0