Mahesh Mahesh - 1 year ago 164
C Question

Pointer to #define

I was just curious to know if it is possible to have a pointer referring to #define constant. If yes, how to do it?

Answer Source

The #define directive is a directive to the preprocessor, meaning that it is invoked by the preprocessor before anything is even compiled.

Therefore, if you type:

#define NUMBER 100

And then later you type:

int x = NUMBER;

What your compiler actually sees is simply:

int x = 100;

It's basically as if you had opened up your source code in a word processor and did a find/replace to replace each occurrence of "NUMBER" with "100". So your compiler has no idea about the existence of NUMBER. Only the pre-compilation preprocessor knows what NUMBER means.

So, if you try to take the address of NUMBER, the compiler will think you are trying to take the address of an integer literal constant, which is not valid.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download