8

For :

int *a;

a is an address where an integer can be stored. &a is an address where a is stored. Then, where is &a stored? And, where is &(&a) stored? And, where is &(&(&a)) stored? Where does this storing of addresses stop?

jjnguy
  • 136,852
  • 53
  • 295
  • 323
simplfuzz
  • 12,479
  • 24
  • 84
  • 137
  • 1
    An integer can't be stored there until you allocate some memory, or assign the address of some valid memory to a – Nick Meyer Jul 10 '09 at 14:30
  • If you are really looking for how pointers are stored, check out this answer to: http://stackoverflow.com/questions/991075/does-a-pointer-also-have-any-address-or-memory-allocation/991152#991152 – Robert Cartaino Jul 10 '09 at 15:00
  • Here too: http://stackoverflow.com/questions/96285/in-c-i-cannot-grasp-pointers-and-classes/98525#98525 – 17 of 26 Jul 10 '09 at 15:20
  • There's a great explanation of pointers here: http://stackoverflow.com/questions/5727/ – Bruce Alderman Jul 10 '09 at 20:38

14 Answers14

7

If you don't explicitly write &a it will not be stored anywhere. If you do write then the address will be computed and stored either in an unnamed variable (temporary) or a named varible you write.

For example:

functionCall( &a ); // address will be in a temporary variable used for passing the parameter
int** b = &a; // address will be stored in variable b
otherFunctionCall( &&a ); // illegal, since &a is an expression operator & can't be applied to it
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Even if I don't write &a, there must be some place where a is actually stored? – simplfuzz Jul 10 '09 at 14:39
  • @dta: I'm not certain I'm understanding your question here... `a` is stored on the stack, just like any other local variable. When you write `&a`, the program computes the address of where in the stack `a` is stored (it already knows the stack pointer because it's a register and it knows the offset into the stack frame because that's compiled in as a constant) and does something with it, as explained by sharptooth. – rmeador Jul 10 '09 at 15:12
  • Well, yeap. You again confuse the variable and the address of the variable. They are usually not equal. The variable is a chunk of memory located at some address and containing some value. – sharptooth Jul 10 '09 at 15:27
  • This helps me a lot.(alongwith sharptooth's answer) http://stackoverflow.com/questions/991075/does-a-pointer-also-have-any-address-or-memory-allocation/991152#991152 – simplfuzz Jul 10 '09 at 16:00
4

&a is a constant.

&(&a) is illegal.

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
  • 1
    constant within that scope =P – DevinB Jul 10 '09 at 14:29
  • 2
    `&a` isn't really a constant. Variable could be on stack. It is computes in runtime. – Kirill V. Lyadvinsky Jul 10 '09 at 14:30
  • 3
    It's an rvalue (i.e., what you'd put on the right side of an assignment operator), and therefore a constant as far as the program is concerned. The fact that it may have different values on different runs is irrelevant. – David Thornley Jul 10 '09 at 14:31
  • 3
    Strictly speaking, &a is a temporary, and hence an r-value, and you can't take the address of an rvalue. You can call it "a constant" or "an elephant" as you please, either way you aren't speaking the language of the standard. – Steve Jessop Jul 10 '09 at 14:35
  • "constant" in english sense, not in the C89 `const` / `literal` sense. – J-16 SDiZ Jul 10 '09 at 14:35
  • &a is like the result of a function call. It's just a value. No object involved, and no change allowed (obviously, a value cannot be changed! An object can be changed that will later produce a different value when re-read). – Johannes Schaub - litb Jul 10 '09 at 14:46
  • &a is not constant. The value will change everytime the code containing &a is called. Again, defined at runtime. It's the memory location of the pointer to an integer. – Kieveli Jul 10 '09 at 18:10
  • &a is constant in regards to the fact that you cannot assign to it. This is like a constant argument to a function. Although it changes value each time you see it, you can't make it be anything you want. That's decided elsewhere. for a function argument, the caller decides. for this particular expression, it's determined by where a actually is. – SingleNegationElimination Jul 12 '09 at 03:45
4

a is not "an address where an integer can be stored". a is a variable large enough to hold the address of an integer. The only "integer" you can store directly in a is the address of an integer, viewed as an integer itself:

int *a;
int b;

a = &b;

printf("a is now %x\n", (unsigned int) a);

It is correct that a itself has an address, which is &a, but that address is not stored somewhere explicit, at runtime.

At a stretch, you might be able to store something that looks like the integer 0:

a = 0;

But this is just a shorthand syntax for "the NULL pointer", i.e. a pointer value guaranteed to not be the address of any actual object.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • This answer isn't perfect...but it's a definite +1 as I think it's definitely got the best chance of explaining/answering the original question for the asker. – Beska Jul 10 '09 at 14:44
3

&a is the address of a. It is a value, result of operator & applied to a, and is not "stored", and has no address, so &(&a) is invalid. It's like 2+3.

Eric Bainville
  • 9,738
  • 1
  • 25
  • 27
2

int *a is a variable the size of a pointer, just like int b would an automatic int variable.

If this declaration is in a function, that variable is automatic and stored on the [stack](http://en.wikipedia.org/wiki/Stack_(data_structure)#Hardware_stacks) at runtime (a simple stack decrement allocates memory for it).

If the declaration is global, then 'a' is simply mapped in executable's .DATA area.

Any more & signs appended can 'create storage', because of the temporary variables you're using to hold'em ;) :

b = &a; //the address in the executable's .DATA or on the stack (if `a` auto)
c = &b; //the address of `b` on the stack, independent of `a` or `&a`
d = &c; //the address of `c` on the stack, independent of `a` or `&a`
z = &(&a); //error: invalid lvalue in unary '&'

The last line complains about the fact that & requires the operand to be a lvalue. That is, something assignable - like b and c above. (&a) as is a result of an expression which is not stored anywhere, therefore is not a lvalue.

Vlagged
  • 503
  • 5
  • 17
1

You can keep going forever:

int value = 742;
int *a = &value;
void *b = &a;
void *c = &b;
void *d = &c;

You wouldn't put it on a single line without assigning it to anything - in that case it would be invalid.

Kieveli
  • 10,944
  • 6
  • 56
  • 81
  • Yes, but using void is generally frowned upon. It would be better to write int **b, int ***c, int ****d, etc. – Dima Jul 10 '09 at 14:38
1

At the crux of your problem seems to be a lack of understanding of the physical nature of memory and pointers. Not how the code works. As Im sure you know, physical memory is comprised of a large group of adjacent cells. The addresses of these cells are fixed and hard-coded by the computer itself, not by software apps or the programming language that you use. When you refer to &a, you are referring to the physical block of memory that is currently holding your value you've stored within the computers ram. "a" is simply a name that you've given the computer so that it knows exactly what block of memory to find the value that you've stored. I think that pretty much covers memory address.
Lets go over pointers now. A pointer is yet another memory address, that is referred to by the computer. It has whatever name that you give it. In this case it should be called something else besides the same name that you gave your first value. Lets call it "b". Based on how you declared it. b's memory location is only capable of holding one type of data....another memory location.... so when I say: b= &a I'm saying that the memory address of 'b'(which is designed only to hold memory addresses), is to hold the memory address of 'a'. Meanwhile on the other side of town, the memory address of 'a' has an integer stored in it.

I hope that this didnt get confusing, I tried not to get all techno-babble on you here. If youre still confused. Post again, Ill explain with code next time.

-UBcse

KodeZero
  • 21
  • 1
1

In C, a variable x may act as a value (on the right hand side of =, where it is called an rvalue), or it may act as a container for values (on the left hand side of =, where it is called an lvalue). You may take the address of x, because you can take the address of any lvalue—this gives you a pointer to the container. But because a pointer is an rvalue, not a container, you can never take &(&x). In fact for any lvalue l, &l is legal but &(&l) is never legal.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
0

a is a variable of type "address of int"; &a is the address of variable a; &(&a) would be the address of the address of variable a, which makes no sense

Yohnny
  • 86
  • 4
0

Not quite. a is a variable in which an address of some integer may be stored. &a is the address of a, i. e. the address of the variable a, which may contain an address of some integer.

Very Important: until and unless an address of something is assigned to a, it is an uninitialized pointer. Trying to use whatever it points to will lead to unpredictable results, and will likely crash your program.

Dima
  • 38,860
  • 14
  • 75
  • 115
0

You can have a pointer to a pointer.

Ex:

void foo(int **blah)
{
 int *a = *blah;

 ...
}

A pointer does take up memory. It's just a small container that holds the address of something. It just can't take up "no space" because everything in the computer is represented somehow by numbers. It's just that as far as C/C++ is concenred, int *a is simply a pointer to an object and takes up no space. That is to keep you from having to manage any sort of memory... it keeps the machine seperated from the code.

Daniel
  • 374
  • 2
  • 5
  • A pointer certainly takes up memory. int *a; allocates a local variable on the stack, which takes up however many bytes is needed to store a pointer on your machine. – Dima Jul 10 '09 at 14:36
0

int *a; is a pointer to an int called 'a'. &a; is the derefrence of int *a. it's pointing to itself. this is what you would use to point to the variable that you wanted to pass around from function to function. derefrence is just a fancy word for "getting the address back" &(&(&a)) is not a valid expression as previously stated. you may make a pointer to a pointer to a pointer. That may be what your thinking of. In such a case you would derefrence the last pointer in question and the computer should understand what you're talking about.

To answer the "where is 'a' stored" question; on the stack.

please, if i'm incorrect on anything, let me know.

mikeyickey
  • 37
  • 1
  • 8
0

&a is a number which is an rvalue: you can store it somewhere if you want to in a variable you will have declared or allocated, of type int*.

To wit:

int a = 42;
&a; /* this does not store the address of a because you've not assigned the value to a variable */
int **aptr = &a; /* aptr is on the stack */
int **aptr2 = (int*)malloc(sizeof(int*));
aptr2 = &a; /* aptr2 is in the heap */

&(&a) is not legal syntax. If you want a pointer to a pointer to an int:

int b = 39;
int *bptr = &b;
int **ptr2bptr = &bptr;

You have to build up the levels of indirection.

With the above you can then do this if you want:

printf("%d\n", *aptr);
printf("%d\n", *aptr2);
printf("%d\n", *bptr);
printf("%d\n", **ptr_to_bptr);

Producing output of:

42
42
39
39
Welbog
  • 59,154
  • 9
  • 110
  • 123
0
int* a;

This line simply declares a pointer to an integer. That pointer has a memory location, which you can get the address of using &a. & is an operator that returns the address of whatever it is run on. But if you do not assign this value anywhere, there is no further &-ing possible.

As to your question as to where &a is stored, most likely in a register. If you do not use the value, it will be immediately discarded. (And registers do not have memory addresses, which is why you cannot do &(&a))

muusbolla
  • 637
  • 7
  • 20