简体   繁体   中英

Can I dereference the address of an integer pointer?

I am trying to figure out all the possible ways I could fill in int pointer k considering the following givens:

int i = 40;    
int *p = &i;    
int *k = ___; 

So far I came up with "&i" and "p". However, is it possible to fill in the blank with "*&p" or "&*p"?

My understanding of "*&p" is that it is dereferencing the address of an integer pointer. Which to me means if printed out would output the content of p, which is &i. Or is that not possible when initializing an int pointer? Or is it even possible at all anytime?

I understand "&*p" as the memory address of the integer *p points to. This one I am really unsure about also.

If anyone has any recommendations or suggestions I will greatly appreciate it! Really trying to understand pointers better.

Pointer Basics

A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int i = 40; , a pointer (eg int *p = &i; ) would simply hold the address where 40 is stored in memory.

If you need the value stored at the memory address p points to, you dereference p using the unary '*' operator, eg int j = *p; will initialize j = 40 ).

Since p points to the address where 40 is stored, if you change that value at that address (eg *p = 41; ) 41 is now stored at the address where 40 was before. Since p points to the address of i and you have changed the value at that address, i now equals 41 . However j resides in another memory location and its value was set before you changed the value at the address for i , the value for j remains 40 .

If you want to create a second pointer (eg int *k; ) you are just creating another variable that holds an address as its value. If you want k to reference the same address held by p as its value, you simply initialize k the same way you woul intialize any other varaible by assigning its value when it is declared, eg int *k = p; (which is the same as assigning k = p; at some point after initialization).

Pointer Arithmetic

Pointer arithmetic works the same way regardless of the type of object pointed to because the type of the pointer controls the pointer arithmetic, eg with a char * pointer, pointer+1 points to the next byte (next char ), for an int * pointer (normal 4-byte integer), pointer+1 will point to the next int at an offset 4-bytes after pointer . (so a pointer, is just a pointer.... where arithmetic is automatically handled by the type )

Chaining & and * Together

The operators available to take the address of an object and dereference pointers are the unary '&' ( address of ) operator and the unary '*' ( dereference ) operator. '&' in taking the address of an object adds one level of indirection . '*' in dereferening a pointer to get the value (or thing) pointed to by the pointer removes one level of indirection . So as @KamilCuk explained in example in his comment it does not matter how many times you apply one after the other, one simply adds and the other removes a level of indirection making all but the final operator superfluous.

( note: when dealing with an array-of-pointers , the postfix [..] operator used to obtain the pointer at an index of the array also acts to derefernce the array of pointers removing one level of indirection)

Your Options

Given your declarations:

int i = 40;    
int *p = &i;    
int *k = ___;

and the pointer summary above, you have two options, both are equivalent. You can either initialize the pointer k with the address of i directly, eg

int *k = &i;

or you can initialize k by assinging the address held by p , eg

int *k = p;

Either way, k now holds, as its value, the memory location for i where 40 is currently stored.

I am a little bit unsure what you're trying to do but,

int* p = &i;

now, saying &*p is really just like saying p since this gives you the address.

Just that p is much clearer.

The rule is (quoting C11 standard footnote 102 ) that for any pointer E

&*E is equivalent to E

You can have as many &*&*&*... in front of any pointer type variable that is on the right side of = .

With the &*&*&* sequence below I denote: zero or more &* sequences. I've put a space after it so it's, like, somehow visible. So: we can assign pointer k to the address of i :

int *k = &*&*&* &i;

and assign k to the same value as p has:

int *k = &*&*&* p;

We can also take the address of pointer p, so do &p , it will have int** - ie. it will be a pointer to a pointer to int . And then we can dereference that address. So *&p . It will be always equal to p .

int *k = &*&*&* *&p;

is it possible to fill in the blank with "*&p" or "&*p"?

Yes, both are correct. The *&p first takes the address of p variables then deferences it, as I said above. The *&variable should be always equal to the value of variable . The second &*p is equal to p .

My understanding of "*&p" is that it is dereferencing the address of an integer pointer. Which to me means if printed out would output the content of p, which is &i. Or is that not possible when initializing an int pointer? Or is it even possible at all anytime?

Yes and yes. It is possible, anytime, with any type. The &* is possible with complete types only.

Side note: It's get really funny with functions. The dereference operator * is ignored in front of a function or a function pointer. This is just a rule in C. See ex. this question . You can have a infinite sequence of * and & in front of a function or a function pointer as long as there are no && sequences in it. It gets ridiculous:

void func(void);
void (*funcptr)(void) = ***&***********&*&*&*&****func;
void (*funcptr2)(void) = ***&***&***&***&***&*******&******&**funcptr;

Both funcptr and funcptr2 are assigned the same value and both point to function func .

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