Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to #define

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

like image 994
Mahesh Avatar asked Dec 17 '10 05:12

Mahesh


People also ask

Can a pointer point to a value?

Put another way, the pointer does not hold a value in the traditional sense; instead, it holds the address of another variable. A pointer "points to" that other variable by holding a copy of its address. Because a pointer holds an address rather than a value, it has two parts. The pointer itself holds the address.

What is pointer point?

As just seen, a variable which stores the address of another variable is called a pointer. Pointers are said to "point to" the variable whose address they store.

Can a pointer point to another pointer?

Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x . Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer.

What is pointer to pointer with example?

A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.


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

like image 95
Charles Salvia Avatar answered Oct 23 '22 20:10

Charles Salvia