?
Which of the following statements are true after execution of the program.
Which of the following statements are true after execution of the program.
void main()
{
int a[10], i, *p;
a[0] = 1;
a[1] = 2;
p = a;
(*p)++;
}
void main()
{
int a[10], i, *p;
a[0] = 1;
a[1] = 2;
p = a;
(*p)++;
}
Answer & Solution
Correct Answer:
Option
B
After the execution of the program, the value of the first element of the array 'a' will be modified. Let's break down the code step by step:
a[0] = 1;: Sets the first element of the array 'a' to 1.
a[1] = 2;: Sets the second element of the array 'a' to 2.
p = a;: Assigns the address of the first element of the array 'a' to the pointer 'p'.
(*p)++;: Increments the value at the memory location pointed to by 'p'. Since 'p' points to the first element of 'a', it increments the value of 'a[0]'.
After the execution of the program, the value of a[0] will be 2.
Therefore, the correct statement is Option B: a[0] = 2.
a[0] = 1;: Sets the first element of the array 'a' to 1.
a[1] = 2;: Sets the second element of the array 'a' to 2.
p = a;: Assigns the address of the first element of the array 'a' to the pointer 'p'.
(*p)++;: Increments the value at the memory location pointed to by 'p'. Since 'p' points to the first element of 'a', it increments the value of 'a[0]'.
After the execution of the program, the value of a[0] will be 2.
Therefore, the correct statement is Option B: a[0] = 2.
Join the Discussion
Login to post a comment or share your explanation.
Login{
int a[10], i, *p;
a[0] = 1; // suppose base address is 1000
a[1] = 2; // address of 1 index will be 1004
p = a;//will contain base address of array so pointer will contain base address(1000);
(*p)++;//*p mean value at address which contain by the pointer p(contain address 1000 means value at this address=1).
//It will replace like (1)++ or 1++ means post increment operator that meaning is use first then increase.
Hence output will be 1.
}