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?

5
  • 1
    You may want to have a look at stackoverflow.com/questions/1637332, which talks about the differences between define and constants. Commented Dec 17, 2010 at 5:25
  • 1
    You can use -E (g++ -E file.cc) to see your file after preprocessing step. Commented Dec 17, 2010 at 5: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. Commented Dec 17, 2010 at 5: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? Commented Dec 17, 2010 at 7: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. Commented Aug 31, 2023 at 14:20

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

4 Comments

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.
#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.
Thanks but I don't have enough reputation to award you points. Sorry !
@Mahesh : don't worry Mahesh; I gave him +1 on behalf of you :D
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.

Comments

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.

2 Comments

It is hardly a constant then, is it?
OK, the OP has left the building: "Last seen more than 6 years ago". Perhaps somebody else can chime in?
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.

3 Comments

Re "You can get full information from above two links.": No, the link is effectively broken: "Visual Studio 2005 Retired documentation"
OK, the OP has left the building: "Last seen more than 5 years ago"
0
#include <stdio.h>

#define MM 0xAA

void f1(unsigned char *p)
{
  printf("%d", *p);
}

void main()
{
  f1(&(unsigned char) {MM});
}

1 Comment

Welcome to StackOverflow! Please take the tour to learn how this site works, and read "How to Answer". Then come back and edit your answer to explain how this answers the question. Source-only answers are frowned upon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.