Examveda

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)++;
}

A. a[1] = 3

B. a[0] = 2

C. a[1] = 1

D. a[0] = 3

E. Compilation error

Answer: Option B

Solution (By Examveda Team)

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.

This Question Belongs to C Program >> Pointer

Join The Discussion

Comments (3)

  1. Ajay Maurya
    Ajay Maurya:
    2 years ago

    void main()
    {
    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.
    }

  2. Devika Patil
    Devika Patil:
    3 years ago

    acccording to precednace 1st bracket will evaluate..*P is 1....now post icrement ++ will perform,so value become 2..*P=2

  3. Bhaumik Patel
    Bhaumik Patel:
    8 years ago

    *p++ =(*p)++ ans will be same then how the value change in above equation

Related Questions on Pointer