简体   繁体   中英

Different ways to assign pointer in C , using & or *?

In C , if i want a pointer reference to a variable

int c = 12 ; int *p ; p = &c ;

or i can do it this way

int c = 12; int p; p=&c; in both case value of p is the address of c , can you please tell the problems i will be facing .

You cannot do it this way:

int c = 12;
int p;
p = &c;

This is not valid C to assign a pointer value to an integer object. Enable all your compiler warnings, the compiler has to give a diagnostic message for the invalid assignment.

In the first case, there is no problem as p is a special type of variable which can contain address. Thus here p is called a pointer variable.

In second case, p is normal scalar variable which cannot contain address. So there is a problem. Compiler implicitly will not be able to assign the address of c variable to the variable p

& and * mean different things in different contexts.

& in a variable declaration (including in a function parameter) means "reference" or "by reference" in C++, and is not allowed in C. In C++, the type of j below is "int". It doesn't modify the type, but says "this is another name for existing data" rather than "create a space for new data".

int i = 5;
int &j = i; //C++ only: j is another name for i
int f(int & x); //f is a function that takes in an int by reference

* in a variable declaration means "pointer". The type of int* is "pointer to an int", while, again, the type of int& (C++ only) is int . It modifies the type.

int *p = NULL; //p is a pointer to an int, that currently points to nothing.
int f(int & x); //f is a function that takes in an int by reference

& in front of an existing variable means "a pointer to", or "address of". It's an operator, which can be thought of as a special kind of function. It takes in anything and returns a pointer to that thing.

int i = 5;
int *p = &i; //p points to i
int **pp = &p; //pp points to p

* in front of an existing variable means "what this is pointing to", also known as the dereference operator. Like & , it's an operator. It can only be applied to a pointer, and it returns what the pointer is pointing to.

int i = 5;
int *p = &i; //p points to i
int j = *p; //j copies what p is pointing to

So if I say *&var , that is the same as var , because it means "dereference the pointer to var".

int c = 12; int p; p=&c; 

introduces the risk of losing bits from the address of c .

If you really need to store an address as integer then use uintptr_t as target integer type, as it's guaranteed by the C standard to be wide enough to store an address:

int c = 12; uintptr_t p; p = (void*)&c; 

uintptr_t comes in <stdint.h> .

7.18.1.4 Integer types capable of holding object pointers

1

The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

intptr_t

The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

uintptr_t

These types are optional.

However to have this integer converted back to a pointer to an integer it casting needs to go via (void*) :

int * pi = (void*)p;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM