50

Program:

#include<stdio.h>

int main(void) {
    int x[4];
    printf("%p\n", x);
    printf("%p\n", x + 1);
    printf("%p\n", &x);
    printf("%p\n", &x + 1);
}

Output:

$ ./a.out
0xbff93510
0xbff93514
0xbff93510
0xbff93520
$

I expect that the following is the output of the above program. For example:

x        // 0x100
x+1      // 0x104  Because x is an integer array
&x       // 0x100  Address of array
&x+1     // 0x104

But the output of the last statement is different from whast I expected. &x is also the address of the array. So incrementing 1 on this will print the address incremented by 4. But &x+1 gives the address incremented by 10. Why?

6
  • 3
    It seems that &x+1 gives you the address after the array memory (4*4=16 or 0x100)...
    – alnet
    Nov 18, 2015 at 9:09
  • 30
    This is one of those cases where you clearly see the difference between a pointer and an array. Nov 18, 2015 at 9:15
  • 15
    Important clarification: those addresses are in hex. "Incremented by 4" means a 0x4 increment as well, but "incremented by 0x10" means "incremented by 16," not by 10.
    – kdbanman
    Nov 18, 2015 at 17:36
  • 2
    @LukePark It should actually be of type int (*)[4] Nov 18, 2015 at 21:54
  • 1
    Just a note (unrelated to the question you are asking): Your program has undefined behaviour (at least under C99), because you need to cast the pointers to void * before passing them to printf (because it is a variadic function). See Printing pointers in C.
    – sleske
    Nov 19, 2015 at 10:09

5 Answers 5

64
x -> Points to the first element of the array.
&x ->Points to the entire array.

Stumbled upon a descriptive explanation here: http://arjunsreedharan.org/post/69303442896/the-difference-between-arr-and-arr-how-to-find

SO link: Why is arr and &arr the same?

26

In case 4 you get 0x100 + sizeof x and sizeof x is 4 * sizeof int = 4 * 4 = 16 = 0x10.

(On your system, sizeof int is 4).

1
  • 1
    Added the sizeof int step as it varies from system to system.
    – Bathsheba
    Nov 18, 2015 at 9:17
9

An easy thumbrule to evaluate this is:

Any pointer on increment points to the next memory location of its base type.

The base type of &x here is int (*p)[4] which is a pointer to array of 4 integers.

So the next pointer of this type will point to 16 bytes away (assuming int to be 4 bytes) from the original array.

3

Even though x and &x evaluate to the same pointer value, they are different types. Type of x after it decays to a pointer is int* whereas type of &x is int (*)[4].

sizeof(x) is sizeof(int)*4.

Hence the numerical difference between &x and &x + 1 is sizeof(int)*4.

It can be better visualized using a 2D array. Let's say you have:

int array[2][4];

The memory layout for array is:

array
|
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+

array[0]        array[1]
|               |
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+

If you use a pointer to such an array,

int (*ptr)[4] = array;

and look at the memory through the pointer, it looks like:

ptr             ptr+1
|               |
+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+

As you can see, the difference between ptr and ptr+1 is sizeof(int)*4. That analogy applies to the difference between &x and &x + 1 in your code.

-6

Believe it or not, the behaviour of your program is undefined!

&x + 1 is actually pointing to just beyond the array, as @i486's answer cleverly points out. You don't own that memory. Even attempting to assign a pointer to it is undefined behaviour, let alone attempting to dereference it.

20
  • 9
    I always thought that this was defined since taking a pointer to one-past the end of an array was defined. I guess it may be different since this isn't an array of int[4]s, it's only one.
    – chbaker0
    Nov 18, 2015 at 11:03
  • 9
    @Bathsheba: Can you cite the standard here? Usually, past the end pointers are well-defined things; why should it not be in this particular case?
    – user1084944
    Nov 18, 2015 at 11:57
  • 5
    @Bathsheba: As Hurkyl mentions, there should be a blurb in the Standard about how "scalar" objects are treated as arrays of size of 1 for the purpose of "past-the-end" pointers. I would expect this to hold in this case (since an array can contain arrays), but the Standard regularly defies my expectations. Nov 18, 2015 at 12:36
  • 6
    @Bathsheba You are not allowed to "look" beyond the array, but IIRC you are explicitly allowed to store that address in a pointer. I think this answer (and others to that question) cover this.
    – TripeHound
    Nov 18, 2015 at 13:59
  • 12
    @Bathsheba C11 6.5.6 p7: "a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one". Not even a note - this is main text. (immediately precedes the stuff about one-past-the-end) No UB here. Scalars are exactly the same as arrays for pointer arithmetic purposes. Nov 18, 2015 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.