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

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.

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

Soumitra said:   1 decade ago
But how the *(*()) works? please explain.

Triven Sharma said:   1 decade ago
I think *(*(a+0)+1)= *(*(a[0])+1)= *(1+1)=2
as a[0]=1

Sudhi said:   1 decade ago
I support sharma.

Rupinderjit said:   1 decade ago
@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:   1 decade ago
How (*a[0]+1),*(*(a+0)+1) can be 2?

Satishp said:   1 decade ago
*(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.


Post your comments here:

Your comments will be displayed after verification.