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.

Sujan said:   1 decade ago
We can refer one dimensional as:

a[i] = *(a+i) = *(i+a) = i[a].

The above four expressions gives same result.

For 2-dimensional array.

s[2][1] = *(s[2]+1) = *(*(s+2)+1).

The above three expressions gives same value.

Abc said:   1 decade ago
a[0] equals *(a+0).

a[0][0] equals*(*(a+0)+0).

So in general a[I][J] == *(*(a+I)+J).

Shashi said:   1 decade ago
First let me clarify some basic things.

1. a[0]==*(a+0).

2. a[0]+1==[a[0]address+next byte(int in this case) address].for example
a[0]=500 and int will take 4 bytes then a[0]+1==500+4=504 is same as[(a+0)+1].

3. *(a[0]+1)== value at(504)==*(*(a+0)+1).

4. *(*(a+0)+1)==value at(3).

H:) said:   1 decade ago
it is a two dimensional array consists of 3 rows and 4 columns so its like:

1 2 3 4
5 6 7 8
9 10 11 12

But memory is allocated like the two dimensional array is also a general array consists of 4 elements.so if refer a[0]=4, a[1]=5, a[2]=9, a[0]+1=2 like that and we know that memory allocated for array is consecutive blocks.so array starts at 1002 next element is at 1006.

Noor said:   1 decade ago
a[0] is similar to *(a+0) which is *a.

R R Beniwal said:   1 decade ago
@Mayank.

a[0]=*a=*(a+0) i.e. all are same.

Here *(a[0]+1) becomes *(*(a+0)+1) that is the value of a[0][1].

If you take example:

int a[3][4] = { 5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

Output will be same.

Don't think it as *(1+1)..think as *(*(a+i)+j) which returns value of a[i][j]... :):).

Sriman said:   1 decade ago
s[i]=*(s+i) and,

i[s]=*(s+i). both are same. Only the way of declaration is different.

Similarly,

a[0] can also write as *(a+0). Both are same.

Mayank said:   1 decade ago
In gcc compiler the code is showing following error:

Warning: format \'%u\' expects argument of type \'unsigned int\', but argument 2 has type \'unsigned int *\' [-Wformat].

Ayesha said:   1 decade ago
Is (a+0) and a[0] is same.

Does *(*(a+0)+1) becomes *(1+1).

Shrinivas Patgar said:   1 decade ago
In 1D array int a[5] = {1,2,3}; a[0] is equal to *(a+0) is same as *(0+a) is same as 0[a] whose value is 1.

In 2D array int a[2][2] = {1,2,3}; a[0][0] is equal to *(a[0]+0) is same as *(*(a+0)+0) whose value is 1.


Post your comments here:

Your comments will be displayed after verification.