3.2k views

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 | 3.2k views
0
at least mark my answer to tell whether it is correct or [email protected]

$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$

by Veteran (431k points)
edited
0
corerct explanation by Arjun
+2
Can someone tell me? how last printf is working?
+1
When we are at the  last statement i.e last  printf we are pointing to the location containing 7 now we have a[-1][1] . Now here we subtract one from the location of 7 and then we proceed . But according to this now the location points to a[0][1]=7 again plz correct me not able to understand how we point to  location of 8
0
a1 starts from 6 and you are printing a1[1] which is 7. What is the issue?
0
i got it sir
0
But as we passing base address of x from main to (int *a[])( which is array of pointer) in print function then a should point to x[] array. Why x is copy to *a[] like call in value ? because x is a base address of *x[]
0
@prachigupta ! Yes it's the same thing where argument print is declaring the fact that the function is expecting an array of pointers to integers and thus a is formal parameter which is also pointing to x only !
+3
I cant understand the 4th print statement   "printf("%d,", *(++a)[0]);"

How can we increment an array? Isnt that illegal? I knew that any array variable is not supposed to be assigned. Could you pls explain?
+1
Same doubt, is there a solution to above doubt?

main()
{
int i[]={-12, 27, -31};
int z[]= {6, 7, 8, 18, 34, 67};
int *q[]={i};
int **x = ++q;

}

even this code is not working
+1

How it is

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

+2
@humblefool In C language when an array is passed to a function, it becomes a pointer.
+1

@Arjun Sir For the last statement I have thought that a[-1][+1] , I added 1 to both , I get a[0][2] which is same as 8. Is this approach right sir ?

0
@sayan after incrementing Q ... which location Q is pointing ???
0
here a[-1][+1] means doing arithmetic operation on current pointer value.

In 4th printf 'a' is made to point a2. So a[-1] means 'a' is now pointing to the preceding element a1. Now at 3rd printf 'a' was made to point 7. So a[+1] means pointing to the successive element i.e  8.

a[-1] = current position - 1

a[+1] = current position + 1

Anyone,correct me if I'm wrong
0
"a1 was incremented in 3rd printf and hence starts from 7 and not 6"

Each time a pointer points to a previously accessed array, it points to the last pointed location? Please clarify :/
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
by (131 points)
0
well explained :)
0
Thank you
+1

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.


by Active (1.9k points)
0

sir, can u pls. xplain the last statement..i.e.....

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


0

i am also in a perplexing situation here.i am working on it.

0
The way to do it would be to show the array address calculation.
0
here in last statement arjun i can see what -1 is doing but +1 should have made itself to the "7" of a1. you have any idea about this.??
+1

But

*++a[0])

was done on third printf