tags:

views:

165

answers:

7

Hey,

What is the result of that line:

int* ptr;
printf("%x, %x\n", ptr, &ptr);

ptr is actually an adress in a memory. So what is &ptr ??

Thanks in advance

+22  A: 

&ptr would be the address for the memory location that ptr is held in. Essentially it is a pointer to a pointer.

ghills
+5  A: 

&ptr returns the address of the pointer variable... pointer to a pointer if you will.

This is often used to allow the function to change where the pointer is actually pointing.

John Weldon
Pete Kirkham
Updated the wording.
John Weldon
+6  A: 

It's the address of the memory location that contains the address of the original memory location (i.e., it's a "pointer to a pointer").

Steve Emmerson
+1  A: 

a pointer is just a reference to the location of some data in memory. *pointer gives you the value stored in that memory location. The & operator returns the actual memory address which, in this case, is a pointer.

Charlie
+1  A: 

In C, pointers are just storage containers that hold an address of some other chunck of data. In this case, ptr holds the address of some int, and it is itself just some piece of data in memory. So &ptr is the address of the variable that hold the address of some int.

goatlinks
pointers aren't just ints. They might be implemented using the same storage size as ints, and addition operations that have essentially the same effect on the bit pattern as for ints. Or they might not. For example, on 64bit Windows, a pointer is twice the size of an int. On segmented memory architectures, a pointer is a "thingy" which can be thought of as representing two separate numbers - the segment and the offset. As far as C is concerned, a pointer is just a "thingy", and any resemblance to an int is implementation-dependent.
Steve Jessop
Right, what I meant by saying that pointers are ints, is that they are just a storage container for some integer value. On most (AFAIK) major OSs (Windows, Linux, OS X), pointers are simply a 32 or 64 bit number that points to some address in memory, and that you can add, subtract, multiply, etc to change what t points to. In this way they are just integer numbers. But the important part of what I was saying is that pointers have an address themselves.
goatlinks
As a side note, gcc (and most C compilers) will only allow you to apply addition and subtraction to pointers, not multiplication and division (since that doesn't really have any meaning w.r.t. pointers).
mipadi
True, I was referring to offsets like `ptr + i*sizeof(int)` which is not actually pointer multiplication.
goatlinks
They aren't just a storage container for an integer value either. Pointer objects are a storage container for a value, all right, but the value isn't (necessarily) an integer. As you've edited to say, it's an address. Since we know computers are made of bits, you can generally find a way of pretending it's an integer, by stringing together the bits and calling that a number. But you can do that for floats too - 2.0 as an IEEE 32bit float is 0x40000000. You wouldn't normally say that floats are "just ints", though. In C, different types are different.
Steve Jessop
Right, when I am dealing with memory, I tend to simplify and abstract the true nature of it, and think about it as a giant array of bytes. These bytes then have a value and an index, and I see the index as an `int`. I know that this is not technically always accurate, but it helps me simplify the problem at hand.
goatlinks
+3  A: 

ptr is not just "an address in memory". ptr is an lvalue, an object in memory that holds an address. Since ptr is an object in memory, it also has its own address. That address is exactly what you get when you do &ptr.

AndreyT
A: 

&ptr could be stored only in int **var or a double pointer variable, so &ptr is nothing but the address of the ptr containing another address.

wrapperm