7

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • 1
    You may want to have a look at http://stackoverflow.com/questions/1637332, which talks about the differences between define and constants. – Tony Delroy Dec 17 '10 at 05:25
  • 1
    You can use -E (g++ -E file.cc) to see your file after preprocessing step. – Julio Guerra Dec 17 '10 at 05:29
  • Ditto what Julio said, for Visual Studio: If you right-click on the project and go to C/C++ -> Preprocessor, you can tell it to create a preprocessed file, the *actual* file fed to the compiler. – user541686 Dec 17 '10 at 05:31
  • EVeryones been addressing the macro issue, but i dont really follow even when const is used. Why would you want to have a pointer to a constant? As for 'how' - can you just reference/dereference to it like any other variable? is there a sneaky way to change a constant using pointer math? – jon_darkstar Dec 17 '10 at 07:27
  • @Peter Mortensen Kindly stop bumping 13 year old posts just to fix a single minor grammar mistake. Your edits are not substantial enough to motivate bumping all these posts. – Lundin Aug 31 '23 at 14:20

5 Answers5

13

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.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
12

No, because #define is for text replacement, so it's not a variable you can get a pointer to. You're seeing it actually being replaced by the definition of the #define before the code is passed to the compiler, so there isn't anything to take the address of. If you need the address of a constant, define a const variable instead (C++).

It's generally considered good practice to use constants instead of macros, because of the fact that they actually represent variables, with their own scoping rules and data types. Macros are global and typeless, and in a large program they can easily confuse the reader (since the reader isn't seeing what's actually there).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user541686
  • 205,094
  • 128
  • 528
  • 886
  • In that case, could you please explain how #define differ from const int in C++ ? I know that both are only read only accessible and should be initialized while being constructed. Is there any other difference that compiler could figure out. – Mahesh Dec 17 '10 at 05:17
  • `#define` is literally like using Find/Replace and then feeding your text to the compiler. A constant variable is a regular variable *during* (rather than before) compile time (with its own scope) that has a value that can't change. Because a `#define` macro isn't a variable (it's just text that will be replaced with something else), nothing exists when the program is running for you to take the address of. – user541686 Dec 17 '10 at 05:18
  • Thanks but I don't have enough reputation to award you points. Sorry ! – Mahesh Dec 17 '10 at 05:20
  • @Mahesh : don't worry Mahesh; I gave him +1 on behalf of you :D – Nawaz Dec 17 '10 at 06:18
3

#define defines a macro. A macro just causes one sequence of tokens to be replaced by a different sequence of tokens. Pointers and macros are totally distinct things.

If by "#define constant" you mean a macro that expands to a numeric value, the answer is still no, because anywhere the macro is used it is just replaced with that value. There's no way to get a pointer, for example, to the number 42.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
0

There is a way to overcome this issue:

#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 #define constant.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

No, it's not possible in C/C++.

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

We are able to use it in two forms.

Please: See this link

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

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

I am sorry. I am unable to provide one more wink... Please see the IBM links... since below I pasted linke link.

You can get full information from the above two links.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1User
  • 614
  • 3
  • 13
  • http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc06lvalue_to_rvalue.htm – 1User Dec 17 '10 at 05:20
  • Re *"You can get full information from above two links."*: No, the link is effectively broken: *"Visual Studio 2005 Retired documentation"* – Peter Mortensen Aug 31 '23 at 14:19
  • OK, the OP has left the building: *"Last seen more than 5 years ago"* – Peter Mortensen Aug 31 '23 at 14:21