Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

in c when we use e.g: a=&*p;doesn't give the address of the pointer.why?

OpenStudy (rsmith6559):

In this context, the ampersand gives the address of p. The asterick dereferences p if p is a pointer. To do what you're thinking should happen: *&p; I have no idea if that will compile. To assign the address of p to a (assuming p isn't a pointer): a = &p;

OpenStudy (anonymous):

I have attached a diagram which I refer to in this answer, have a look. &number gives us the address of the value 5 i.e. &number = 0XF345 (I made this up) Now numberPtr = &number will mean that the value of numberPtr = 0XF345. Now, *numberPtr dereferences the pointer which means we go to the address that numberPtr has, in this example 0XF345 and get the value which is 5 so *numberPtr = 5. If we now do &*numberPtr, this is saying get me the value of *numberPtr (5) and give me it's address which is going to be 0XF345 since that is where 5 is located. Similarly, for &*p, *p gives us the value at the address that p is pointing to, then using &*p will give us the address of the value. If you want the address of the pointer, it is given by &p. In my diagram, &numberPtr would give you 0Z7942. I hope this makes sense.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!