What will be the output of the following C code?
#include <stdio.h>
int main()
{
const int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5;
printf("%d\n", ary[3]);
}
#include <stdio.h>
int main()
{
const int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5;
printf("%d\n", ary[3]);
}
A. 4
B. 5
C. Compile time error
D. 3
Answer: Option B
Here array constant we can't change or modifies value inside it, but we can do using address, we use pointer store the ith element address in pointer ( *ptr = array + 3 => arary + 3*sizeof(int)), after that we deference that address and update ith element in array ( *ptr = 5), it will directly
ary[4] = {1, 2, 3, 5};