edited by
12,761 views
74 votes
74 votes

Which one of the choices given below would be printed when the following program is executed?        

#include <stdio.h>
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
            printf("%d,", a[0][2]);
            printf("%d,", *a[2]);
            printf("%d,", *++a[0]);
            printf("%d,", *(++a)[0]);
            printf("%d\n", a[-1][+1]);
}
main()
{
             print(x);
}

  1. $8, -12, 7, 23, 8$
  2. $8, 8, 7, 23, 7$
  3. $-12, -12, 27, -31, 23$
  4. $-12, -12, 27, -31, 56$
edited by

6 Answers

Best answer
136 votes
136 votes

$a = \{a1, a2, a3\};$

 printf("%d,", a[0][2]);

$a[0]$ is $a1$. So, this will print $a1[2] = 8;$

 printf("%d,", *a[2]);

$a[2]$ is $a3$. So, this will print $*a3 = a3[0] = -12 ([]$ has greater precedence than $*)$

 printf("%d,", *++a[0]);

$a[0]$ which is $a1$ is incremented. $a1$ is a pointer to int (base address of an integer array) and so increment means adding $sizeof(int)$ and hence $a1$ now points to the second element in the array. So, $*++a[0]$ prints second element of $a1$ which is $7$ and now $a1$ starts from $7$.

printf("%d,", *(++a)[0]);

$++a$ will increment $a$, which being a pointer (In C, an array when passed to a function becomes a pointer) to pointer (to int) will add $sizeof (pointer)$ to $a$. So, a now contains $\{a2, a3\}$ and $a[0]$ will be $a2$ and $*a2$ will be the first element in $a2$ which is $23$

printf("%d\n", a[-1][+1]);

$a[-1]$ will subtract a size of pointer from the base address of $a$. Normally this results in invalid memory access, but since we have incremented a previously, $a[-1]$ is valid and will point to $a1$. So, $a[-1][+1]$  will be $a1[1]$ which has the value $8$.
($a1$ was incremented in $3$rd printf and hence starts from $7$ and not $6$. $+1$ is same as $1$, just given to create confusion)

Correct Answer: $A$

edited by
20 votes
20 votes

I Hope This will clear every ones doubt.

Answer will be A

Ans

 

12 votes
12 votes
int *a[]=(a1,a2,a3};
    a is pointing to array a1
    if addition of 1 is performed with a,then it is pointing to next element in a i.e. a2
    a+1 is pointing to array a2
    a+2 is pointing to array a3
    *a is pointing to 0th element in  array a1
    if addition of 1 is performed with *a,then it is pointing to next element in a1
   *( a+1) is pointing to 0th element in array a2
   if addition of 1 is performed with *(a+1),then it is pointing to next element in a2
   *(a+2) is pointing to 0th element in array a3
     
1)a[0][2] = *(a[0]+2)=*(*(a+0)+2)
        a+0 is pointing to array  a1
       *(a+0) is pointing to 0th element in a1
       *(a+0)+2 is pointing to 2nd element in a1
       *(*(a+0)+2) is return the value of 2nd element in a1.      
First printf print 8
 
2)*a[2]=*(*(a+2))

        a+2 is pointing to array a3
        *(a+2) is pointing to 0th element in a3
        *(*(a+2))  return the value of 0th element in a3.      
second  printf print -12

3)*++a[0]=*(++a[0])=*(++(*(a+0)))

        a[0] is pointing to 0th element in a1
        ++a[o]  - after pre increment performed  a1={7,8,18,34,67} now a[0] is pointing to oth element in a1,
        *++a[0]  is return the value of oth element in a1.      
Third  printf print 7

4)*(++a)[0]

  ++a - after pre increment performed a={a2,a3}
  (++a)[0]  is pointing to 0th element in a2
  *(++a)[0]  is return the value of oth element in a2.      
Fourth  printf print 23

5)a[-1][+1]=*(*(a-1)+1)
        a-1 is  pointing to a1 // this portion of memory is valid if only that portion of memory is not allocated to other stack
        frame
       *(a-1) is is pointing to 0th element in a1
       *(a-1)+1 is  pointing to 1st  element in a1
        *(*(a-1)+1) is return the value of 1st element in a1 i.e 8.     because a1 was incremented in 3rd printf
Fourth  printf print 8
5 votes
5 votes

answer is 

8,-12,7,23,8, i.e option A

explanation:

 a[0][2])=a-->a[0]-->a[0][2](way of understanding)=>a1[2]=8

and similarly for others as well.i can explain further if you are not getting it proper.


 
Answer:

Related questions