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 1 of 7.

Alim Ul Karim said:   1 decade ago
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

Rohan said:   7 years ago
@All.

#include<stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

/* (i) int a[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
-> 2-D array with 3 rows and 4 columns.
-> a[0] [0]= {1}, a[0][1] = {2} , a[0][2] = {3} ,a[0] a[3] = {4}....
and so on........
*/
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));

/* (i) a[0] -> memory location begins at 1002
a[0] +1 -> 1002 +4 = 1006 (memory location)

(ii) a[0]+1 -> currently at the memory location
*( a[0]+1 ) -> element at a[0][1] = 2

(iii) *(a+0) -> a[0]
*(a+0)+1 -> a[0][1]
*(*(a+0)+1)) -> *(a[0][1]) = element at a[0][1] = 2

*/
return 0;
}
(5)

Swetha said:   9 years ago
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).

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.

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

LOL said:   1 decade ago
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.

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].

Govind said:   1 decade ago
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);
}
(1)

Tiwari said:   9 years ago
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]

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.


Post your comments here:

Your comments will be displayed after verification.