I have a problem with understanding arithmetic on void pointers,suppose we have this pointer:
int x = 10;
int* p = &x;
so if we want get the address of x we printf p and if we wanna get the value of that address we just printf *p so everything is okay till now, but when i saw this example:
#include<stdio.h>
int main()
{
int a[2] = {1, 2};
void *ptr = &a;
ptr = ptr + sizeof(int);
printf("%d", *(int *)ptr);
return 0;
}
the result of the program is 2 and i dont know why ?
why not the program prints zero as a result , because this line means:
ptr = ptr + sizeof(int);
new address of(ptr) = the current address of(ptr) + 4(size of int)
and the value of void pointer is 1 so the shift will be x[5] and its not exist and it must print 0 as a result, why it prints 2 ?
this is the page that i get the example from :