Pointer to #define

I was just curious to know if there is a pointer to the #define constant. If so, how to do it?

+4
source share
5 answers

The #define directive is a preprocessor directive, which means that it is called by the preprocessor before anything is even compiled.

Therefore, if you type:

#define NUMBER 100

And then you enter:

int x = NUMBER;

What your compiler sees is simple:

int x = 100;

Basically, as if you opened your source code in a word processor and found find / replace to replace each occurrence of "NUMBER" with "100". Therefore, your compiler has no idea about the existence of NUMBER . Only the precompiler preprocessor knows what NUMBER means.

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

+10
source

No, because #define intended to replace text, so this is not a variable that you can get a pointer to - what you see is actually replaced by the definition of #define before the code is passed to the compiler, so there is nothing to accept. If you need a constant address, define the const variable (C ++) instead.

It is generally considered good practice to use constants instead of macros because they actually represent variables, with their own rules and data types. Macros are global and non-trivial, and in a large program you can easily confuse the reader (because the reader does not see what is really there).

+11
source

#define defines a macro. A macro simply replaces one sequence of tokens with another sequence of tokens. Pointers and macros are completely different things.

If by β€œ #define constant” you mean a macro that expands to a numeric value, there is still no answer, because at any place where the macro is used, it is simply replaced with this value. There is no way to get a pointer, for example, to the number 42 .

+3
source

No, this is not possible in C / C ++

You can use the #define directive to give a meaningful name to a constant in your program.

We can use in two forms.

Please: View this link

http://msdn.microsoft.com/en-us/library/teas0593%28VS.80%29.aspx

The #define directive may contain an object-oriented definition or a function-like definition.

I'm sorry I could not provide another hint ... Please check out the IBM links .. after I inserted the linke link

u can get full information over 2 links

0
source

There is a way to overcome this problem:

 #define ROW 2 void foo() { int tmpInt = ROW; int *rowPointer = &tmpInt; // ... } 

Or, if you know its type, you can even do this:

 void getDefinePointer(int * pointer) { *pointer = ROW; } 

And use it:

 int rowPointer = NULL; getDefinePointer(&rowPointer2); printf("ROW==%d\n", rowPointer2); 

and you have a pointer to the constant #define.

0
source

Source: https://habr.com/ru/post/1332447/


All Articles