C Programming - Pointers - Discussion

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

[A]. 448, 4, 4
[B]. 520, 2, 2
[C]. 1006, 2, 2
[D]. Error

Answer: Option C

Explanation:

No answer description available for this question.

Afri said: (Jul 18, 2010)  
The formula used is *(*(a+i)+j).i represents the row and j the column.

N Prathyusha said: (Jul 27, 2010)  
Please clear me the doubt that int is declared 4bytes.

How (*a[0]+1),*(*(a+0)+1) can be 2?

Shree said: (Jul 28, 2010)  
I think 1006 is the memory location but why can't it be 520.

Gowthami said: (Aug 3, 2010)  
Please clear information about problem.

Sivakumaran said: (Aug 13, 2010)  
a[0]+1= 1002+4=> 1006

*(a[0]+1) = 2

*(*(a+0)+1)=???

Sowmya said: (Aug 22, 2010)  
The array begins at the location 1002 and each integer occupies 4bytes. a[0]+1 =(1002+1(is nothing but 4 bytes)) = 1006

*(a[0]+1): This is nothing but the value of the 1st element +1(since * operator is used we are accessing the value). So 1+1=2.

Preethi said: (Oct 15, 2010)  
a[0]+1= 1002+4=> 1006

*(a[0]+1) = 2

*(*(a+0)+1)=> I think (a+0) and a[0] is same.

So a[0]+1 the next element is being getting pointed.

Jithin Mathew said: (Feb 8, 2011)  
Thank you all.

Anonymous said: (Feb 14, 2011)  
*(a[0]+1):

a[0]+1 = 1002+4 = 1006

The value at 1006 ie, the second position is 2.

Habib said: (Apr 18, 2011)  
Why cant a[0] be 1, it represents the rvalue, not lvalue I guess !

Lol said: (May 6, 2011)  
This is a tricky question. You need to first understand how C compiler actually deals with multi-dimensional arrays. For the above example:

int a[3][4] the compiler first creates an array with 3 pointers. These pointers points to another array that holds 4 ints.

By calling a[0] you are accessing the first array which stores the pointer to the array of 4 int that represents row 0!!!!!

So in simple terms a[i] returns the address of each row not the value.

Pankaj said: (May 6, 2011)  
Here *(*(a+0)+1)
in this expiration a+0=a is the starting adders of array *a And now add the value of inter 4 so it will same expiration as second

Shahizzz Rulezzz said: (May 13, 2011)  
We can write *(a+0)as a[0] bcoz a[i]=*(a+i).
so *(*(a+0)+1)further can be writen as *(a[0]+1).
as a[0]=1002 so *(1002+4)
now it will return the value at memory location 1006 n dat is 2.

So *(*(a+0)+1)=2.

Alim Ul Karim said: (Jul 18, 2011)  
As @arif said:
"The formula used is *(*(a+i)+j).i represents the row and j the column."

When we locate a array;

int a[3][4] = {
{1, 2, 3, 4},
{5, 6 ,7, 8 },
{9 ,10, 11, 12}
};
if we say only 'a' or 'a[0]' it locates or returns the memory location of 'a' array.

So for the first answer , memory location of 'a' starts from 1002, hence from that
"a[0]+1" will be 1002 + 4 [because int is 4 bytes length]

And then for the *(a[0]+1), *(*(a+0)+1) we are getting the value from that memory location , which is index as a array and can be retrieve from the @arif's concept.

*(a[0]+1) implements:
*(first memory location start point + 1) means the second index of the array which is a[0][1] value = 2


*(*(a+0)+1) <=> *(*(a+row)+col)) implements:
row = 0 , col =1 , which is a[0][1] value = 2

Soumitra said: (Aug 8, 2011)  
But how the *(*()) works? please explain.

Triven Sharma said: (Sep 4, 2011)  
I think *(*(a+0)+1)= *(*(a[0])+1)= *(1+1)=2
as a[0]=1

Sudhi said: (Sep 13, 2011)  
I support sharma.

Rupinderjit said: (Nov 28, 2011)  
@Sidhi:No Mr. Sharma is wrong.You better get yourself familiar with araay indexing and pointer arithmetic concepts.

a[i](array indexing)==*(a+i)(Pointer arit.).Both points to same element of an array(Hers the case is for 1-D array).

for 2-D:
a[i][j]==*(*(a+i)+j)

Satishp said: (Dec 2, 2011)  
How (*a[0]+1),*(*(a+0)+1) can be 2?

Satishp said: (Dec 2, 2011)  
*(a[0]+1)-> it is increment in the address of the arry 0 to 1.

*(*(a+0)+1)-> it is the arry is added to 0.and incemented by 1.
so the both values of these are 2 and 2.

Kushal said: (Dec 23, 2011)  
*(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: (Jan 8, 2012)  
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: (Jan 16, 2012)  
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: (Feb 17, 2012)  
*(*(a+0)+1)==*(*&a[0]+1) ,now *and & will cancel and
*(a[0]+1)==*(a[0][1])==2.

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

Kamal Nayan said: (Aug 30, 2012)  
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: (Aug 30, 2012)  
@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: (Oct 10, 2012)  
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: (Oct 19, 2012)  
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: (Jan 31, 2013)  
It prints like this in windows only. But in Linux 1010, 2, 2.

Govind said: (Jun 13, 2013)  
What would be the output of the following program assuming that the array begins at location 1002 ?

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

Shrinivas Patgar said: (Jul 31, 2013)  
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.

Ayesha said: (Aug 19, 2013)  
Is (a+0) and a[0] is same.

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

Mayank said: (Sep 29, 2013)  
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].

Sriman said: (Nov 20, 2013)  
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.

R R Beniwal said: (Dec 2, 2013)  
@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]... :):).

Noor said: (Dec 2, 2013)  
a[0] is similar to *(a+0) which is *a.

H:) said: (Dec 11, 2013)  
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.

Shashi said: (Aug 7, 2014)  
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).

Abc said: (Dec 19, 2014)  
a[0] equals *(a+0).

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

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

Sujan said: (Feb 1, 2015)  
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.

Anjum said: (May 14, 2015)  
Why is the address assumed to be 1002 only?

Vamsi Tharun Kumar said: (May 25, 2015)  
Why the address start from 1002 why not 520, any one explain about this addresses?

Prabha said: (Jun 27, 2015)  
We are using format specifiers as %u but instead of address values are printed. Whether it won't return any error?

Swetha said: (Jul 13, 2016)  
In arrays of C programming, the name of the array always points to the first element of an array. Here, address of first element of an array is &arr[0]. Also, arr represents the address of the pointer where it is pointing. Hence, &arr[0] is equivalent to arr.

Also, value inside the address &arr[0] and address arr are equal. Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0] is equivalent to *arr.

Similarly,

&a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).
&a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
&a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
.
.
&a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).

Tiwari said: (Jul 13, 2016)  
A multidimensional array is of form, a[i][j]. Let's see how we can make a pointer point to such an array. As we know now, the name of the array gives its base address. In a[i][j], a will give the base address of this array, even a+0+0 will also give the base address, that is the address of a[0][0] element.

Here is the generalized form for using a pointer with multidimensional arrays.

*(*(ptr + i) + j)
is same as

a[i][j]

Gce said: (Jul 19, 2016)  
But printf using control string %u, then how to prints array elements?

Pratik said: (Jul 23, 2016)  
How to know the address when it is not given. Here the &a[0] is 1002. How can you get that?

Priya said: (Oct 28, 2016)  
We can write a[i]=i[a]
is it work for a[i][j]=?

Soumya said: (Jan 14, 2017)  
Here %u is given instead of %d, %u gives addresS, so would it not display an error?

Vikash Mishra said: (Apr 14, 2017)  
According to me,

a[0]=1002;
and 1=4 bytes //because talking about address not talking about value.
so->1002+4=1006;
*(a[0]+1)=2;

Harshal Shirude said: (May 24, 2017)  
a[0]+1= 1002+4=> 1006.
*(a[0]+1) = 2.
*(*(a+0)+1)=> *(*((1002+0)+1)) => **(1004) => 2 = a[1].

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.