My code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *pi;
short *ps;
pi = malloc(16 * sizeof *pi);
ps = malloc(16 * sizeof *ps);
pi += 8;
ps += 8;
ps = (short*) pi;
*pi = 1000000;
printf("*pi = %d,", *pi);
printf("*ps = %hd\n", *ps);
printf("before incrementing ps : ps = %p\n", ps);
ps++;
printf("after incrementing ps : ps = %p\n", ps);
printf("*ps = %hd\n", *ps);
*ps = 16;
printf("after *ps = %hd -> *pi = %d\n", *ps, *pi);
printf("ps - 1 = %p, -1 + ps = %p\n", ps - 1,-1 + ps);
printf("*(ps - 1) = %hd, *(-1 + ps) = %hd\n", *(ps - 1), *(-1 + ps));
printf("0[ps - 1] = %hd, 1[ps - 2] = %hd, ps[-1] = %hd\n", 0[ps - 1], 1[ps - 2], ps[-1]);
printf("-1[ps] = %hd, -2[ps + 1] = %hd, -3[ps + 2] = %hd\n", -1[ps], -2[ps + 1], -3[ps + 2]);
printf("-3[ps] = %hd, -3[ps + 1] = %hd, -3[ps + 3] = %hd,", -3[ps], -3[ps + 1], -3[ps + 3]);
printf("-3[ps + 4] = %hd\n",-3[ps + 4]);
}
/* format string in printf was adjusted to increase readability */
My output:
*pi=1000000,*ps=16960
before incrementing ps:ps=0xbb9030
after incrementing ps:ps=0xbb9032
*ps=15
after *ps=16->*pi=1065536
ps-1=0xbb9030,-1+ps=0xbb9030
*(ps-1)=16960,*(-1+ps)=16960
0[ps-1]=16960,1[ps-2]=16960,ps[-1]=16960
-1[ps]=0,-2[ps+1]=0,-3[ps+2]=0
-3[ps]=0,-3[ps+1]=0,-3[ps+3]=0,-3[ps+4]=0
I have read that a[b]
is defined as *(a+b)
. Therefore, ps[-1]==*(ps-1)==*(-1+ps)==-1[ps]
. But, as seen in the output, while it is the case that all expressions except the last have the expected value (of 16960
), the last one has the value 0?
Why does this happen? Does -x[ps] produce undefined behavior, causing the compiler to just use 0?
For your information, I use gcc. gcc -v
outputs gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)
.