3

I am just trying to unveil the secrets of C and pointers (once again), and I had a question regarding pointers and decaying. Here is some code:

#include <stdio.h>

int main() {
    int array[] = { 1, 2, 3 };
    int (*p_array)[] = &array;

    printf("%p == %p == %p\n", array, &array, &array[0]);
    printf("%p == %p\n", p_array, &p_array);

    return 0;
}

When I run that, I get this output:

0x7fff5b0e29bc == 0x7fff5b0e29bc == 0x7fff5b0e29bc

0x7fff5b0e29bc == 0x7fff5b0e29b0

I understand that array, &array, &array[0] are all the same, because they decay to a pointer which point to the exact same location.

But how does that apply to actual pointers, here *p_array, which is a pointer to an array of int, right? p_arrayshould point to the location where the first int of the array is stored. But why is p_array's location unequal to &p_array?

Maybe it is not the best example, but I would appreciate if someone would enlighten me...

Edit: p_array refers to the address of the first element of the array, whereas &p_array refers to the address of the p_array pointer itself.

All the best, David

Community
  • 1
  • 1
DeBe
  • 491
  • 3
  • 15
  • `parray` is a pointer variable. Your second example prints the *value* of the pointer variable (where it points to) and its own address (where the variable is stored). Aside: you should cast a pointer to `(void*)` to satisfy the `%p` format. – Weather Vane Dec 26 '15 at 18:21
  • 2
    @erip Well, that question is about C++, this is C. While this particular behavior is the same, in general I don't think a question about a different language can make this a duplicate. – user4520 Dec 26 '15 at 18:27
  • @szczurcio Sure, that's why I don't flag all C questions as dupes to C++ questions or vice versa. The fact that C and C++ treat pointers and references the same is the reason I flagged this. – erip Dec 26 '15 at 18:42
  • @erip Correct me if I'm wrong, but I thought reference variables (like ``&ri = 1``) don't exist in C, only in C++. – DeBe Dec 26 '15 at 18:51
  • @DavidBecher They don't exist in either. – erip Dec 26 '15 at 18:54
  • @erip Have a look here: https://en.m.wikipedia.org/wiki/Reference_(C%2B%2B) - Therefore the question is not a duplicate of your referenced post, since there the user asked about references. – DeBe Dec 26 '15 at 18:56
  • @DavidBecher You sent a link to a wiki page without indicating what to look at. I've removed the dupe suggestion, but this question *has* been asking many, many times before. – erip Dec 26 '15 at 18:59
  • @erip I just don't understand why you are telling me there is no such concept of "references" in C++ (as opposed to C, which has pointers but no references) when there *actually* is such a thing (this is what the link was for). – DeBe Dec 26 '15 at 19:03
  • @DavidBecher You cannot reference a number, you can reference a variable. i.e., `int i = 1; int &ri = i;` is legal in C++. `int &ri = 1;` is legal in neither C nor C++. In any case, the dupe has been removed. This is becoming chat. – erip Dec 26 '15 at 19:05

3 Answers3

3

I understand that array, &array, &array[0] are all the same, because they decay to a pointer which point to the exact same location.

No. You didn't understand. array is of array type and in most cases will convert to pointer to its first element. Therefore, array and &array[0] are same as an expression, except when operand of sizeof operator. &array is the address of array array and is of type int (*)[3]. Read here in details: What exactly is the array name in c?.

p_array is pointer to array array and store its address while &p_array is the address of p_array.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Note that when you print out `&p_array` the address you see is 0xC or 12 less than the other addresses. That is because these addresses correspond to locations on the stack, and `p_array` is located right above (stack grows down) `array`. Since `array` takes up 12 bytes in this case, the difference in the addresses ends up being 12. – fvgs Dec 26 '15 at 18:27
  • That is what I was trying to say - I am aware, that ``array`` is of type ``int[]``, but in most cases decays to a pointer to its first element (with ``sizeof`` being an exception to the rule). I just wrote it that way to simplify ;) But thanks for clarifying my second example, that seems obvious actually. I think I have been reading to much about pointers today, those beasts can be confusing sometimes ;) – DeBe Dec 26 '15 at 18:30
2

But why is p_array's location unequal to &p_array?

&p_array is the address of the pointer itself (0x7fff5b0e29b0), whereas p_array is a pointer to array and its value is the address of array1, (0x7fff5b0e29bc).


1. The address of the first element.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
0

You simply introduced another variable. As such it has a value ( 0x7fff5b0e29bc ) and an address ( 0x7fff5b0e29b0 ). This will be so for any variable you introduce