开发者

Pointer to #define

开发者 https://www.devze.com 2023-01-30 20:24 出处:网络
开发者_如何学JAVAI was just curious to know if it is possible to have a pointer referring to #define constant. If yes, how to do it?The #define directive is a directive to the preprocessor, meaning th
开发者_如何学JAVA

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


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.


No, because #define is for text replacement, so it's not a variable you can get a pointer to -- what you're seeing is actually replaced by the definition of the #define before the code is passed to the compiler, so there's nothing 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 can easily confuse the reader (since the reader isn't seeing what's actually there).


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


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 can able to use 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.

Iam sorry iam unable to provide one more wink ... Please see the IBM links..since below i pasted linke link

u can get full info from above 2 links


There is a way to overcome this issue:

#define ROW 2

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

Or if you know it's type you can even do that:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消