Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why &p+1 give the same result as p

Tags:

c

    #include <stdio.h>
    int main(void)
    {
        int a[]= {15, 14, 34, 46, 69, 86, 10, 0, 4};
        int *p = a; // p point to the first element of array a[0]
        printf("%x\n%x\n",&p+1, &a[0]);
        return 0;
    }

that code will print as example

bff0e554
bff0e554

so &p+1 return the address of the first element of array. As i understand, &p return the address of that pointer and &p+1 should print the address of pointer + 1 please tell me what i missed !!

like image 767
Tel0s Avatar asked Nov 21 '25 06:11

Tel0s


1 Answers

&p takes the address of p, not the value of p. That for you &p + 1 == a is pure coincidence and depends on your stack alignment. Your compiler could have sorted the both variables in another order or put a gap in between.

like image 99
kay Avatar answered Nov 23 '25 21:11

kay