Different Pointer Arithmetic Results when Taking Address of Array

后端 未结 5 2073
北恋
北恋 2020-12-25 10:14

Program:

#include

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


        
相关标签:
5条回答
  • 2020-12-25 10:29

    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).

    0 讨论(0)
  • 2020-12-25 10:29

    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.

    0 讨论(0)
  • 2020-12-25 10:30

    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.

    0 讨论(0)
  • 2020-12-25 10:35
    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?

    0 讨论(0)
  • 2020-12-25 10:40

    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.

    0 讨论(0)
提交回复
热议问题