What is the output the given C language code snippet? #include<stdio.h> int main() { int x[] = {10,20,30}; int *p = x; p++; printf("%d",*p); return 0; }
20
This snippet demonstrates pointer arithmetic on an array. In C, the name of an array (here x) decays to the address of its first element, and adding to a pointer moves it by whole elements, not by single bytes.
Tracing the execution:
int x[] = {10, 20, 30}; — three integers are stored contiguously, at indices 0, 1 and 2.int *p = x; — p is set to the address of x[0], so *p would be 10.p++; — this is the key step. Incrementing an int pointer advances it by sizeof(int) bytes, i.e. by exactly one array element. So p now points to x[1].printf("%d", *p); — dereferencing gives x[1], which is 20.Why the other outputs are wrong: 10 would be printed only if p had not been incremented (still pointing at x[0]). 30 would need two increments to reach x[2], but p++ advances just one element. A garbage value would arise only if p were moved past the end of the array (out of bounds); here it still points to a valid element, so the result is the well-defined value 20.
Which of the following expression will delete the entire array pointed to by q?
Consider the following C program segment.
#include <stdio.h>
int main()
{
char s1[7] = "1234";
char *p;
p = s1 + 2;
*p = '0';
printf("%s", s1);
return 0;
}
#include <stdio.h>
void foo(int *p, int x){
*p = x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z, b);
printf("%d", a);
return 0;
}