C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 12)
12.
What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}
448, 4, 4
520, 2, 2
1006, 2, 2
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
61 comments Page 3 of 7.

Kushal said:   1 decade ago
*(a[0]+1) means it gives *(1002+4)=*(1006)-> that is 2
*(*(a+0)+1) means its equal to the *(a[0][1])

Abhishek.e.k said:   1 decade ago
Let me change the array and make the second element as 9 and compile
#include<stdio.h>

int main()
{
int a[3][4] = { 1, 9, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}

o/p is 1006,9,9

let 1002 be address of 1
let 1006 be address of 9
now try this
just print a --->1002 is the o/p
just print (a+0) --->1002 is the o/p
now the tricky part
when i printed *(a+0) ---->1002 (i got same result as previous)
*(a+0)+1 ---->1006
*(*(a+0)+1)---->9

It means that *(*(a+0)+1)) represent the second term in array that is 9.

Nandy said:   1 decade ago
Guys....
-> *(a[0]+1):a[0] means it will take the value of 1st element in the array bcoz it consisting of *.
* means it will take the object(number or value),so a[0] points to 1,after that it will add to 1(1+1=2)that equals to 2.

->*(*(a+0)+1):*(a+0)=a[0] only so before one and this one z same ans is 2 only

Muzna said:   1 decade ago
*(*(a+0)+1)==*(*&a[0]+1) ,now *and & will cancel and
*(a[0]+1)==*(a[0][1])==2.

Rupa said:   1 decade ago
Can you people pls explain how (*(a[0]+1) and *(*(a+0)+1)gives the same result in @D array??

KAMAL NAYAN said:   1 decade ago
a[3][4]={
1,2,3,4,
5,6,7,8,
9,10,11,12
}

Now,
1. a[0]+1=it means in 1st row 1d-array, add by size of one int(4),so 1002+4 =1 006.

2. *(a[0]+1)= by de-refrence property it will come like
a[0][1], its value is 2.

3. *(*(a+0)+1), again it will dereference like
1st step--> *(a[0]+1) ,again its same like previous
2nd step--> a[0][1], ans will be 2 for this

Hence finally1006 2 2 will be answer

HarshaN said:   1 decade ago
@rupa

for double dimensional arrays.
a[0] = *a = address of a[0][0].
a[1] = *(a+1) = address of a[1][0].
a[2] = *(a+2) = address of a[2][0] and soon.

So
for *(a[0]+1):
a[0]+1 is the address of a[0][1] and pointer to it i.e., *(a[0]+1) would be the element in the a[0][1]

for *(*(a+0)+1):
*(a+0)is the address of a[0][0] and *(a+0)+1 would be the address for a[0][1] and the pointer to it i.e., *(*(a+0)+1) would be the element in the a[0][1].

Gaurav kumar garg said:   1 decade ago
If my program like this
#include<stdio.h>

int main()
{
int a[3][4] = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}

Then *(a[0]+1)=2
Because a[0] represent 0 th index of array.a[0]+1 represent 1st index of array and value at 1st index is 2.

Rookie said:   1 decade ago
In both of the cases its just printing the value at a[0][1] and that's what exactly both the expression means.

Both *(a[0]+1)=a[0][1];
And *(*(a+0)+1)=*(a[0]+1)

As a[i]=*(a+i);

Thanks.

SYED AZAR said:   1 decade ago
It prints like this in windows only. But in Linux 1010, 2, 2.


Post your comments here:

Your comments will be displayed after verification.