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?
-
1You may want to have a look at stackoverflow.com/questions/1637332, which talks about the differences between define and constants.Tony Delroy– Tony Delroy2010-12-17 05:25:29 +00:00Commented Dec 17, 2010 at 5:25
-
1You can use -E (g++ -E file.cc) to see your file after preprocessing step.Julio Guerra– Julio Guerra2010-12-17 05:29:32 +00:00Commented 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.user541686– user5416862010-12-17 05:31:17 +00:00Commented 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?jon_darkstar– jon_darkstar2010-12-17 07:27:39 +00:00Commented 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.Lundin– Lundin2023-08-31 14:20:13 +00:00Commented Aug 31, 2023 at 14:20
6 Answers
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.
Comments
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
#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.#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
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
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
#include <stdio.h>
#define MM 0xAA
void f1(unsigned char *p)
{
printf("%d", *p);
}
void main()
{
f1(&(unsigned char) {MM});
}