#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a=10 ;
int *p;
p = &a;
*(p+1) = 20;
printf("The value at (p+1) is %d\n",*(p+1));
return 0;
}
I’m trying to do some pointer arithmetic and trying to implement the code. I’m trying to assign value 20 to next address in the memory using *(p+1) and print it using the printf statement, but my code isn’t producing any output. Why is it so?
>Solution :
Look at it this way, *(p + 1) is the same as having p[1] this means you are looking for the data in a[1] but a is not an array, a[1] does not exist in your program, we don’t really know what’s in the memory location you are trying to access, that is why your program suffers from undefined behavior.