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.

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.

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

Mohan said:   6 years ago
Why this program works differently in 1 dimensional array?

a+0 gives only the base address (i.e) 1002.

*(a+0) also gives the base address (1002) it wouldn't give the value at address (i.e =>1).
*(a+0)+1 => 1002+4 => 1006.

Now *(*(a+0)+1) = *(1006) here value at address works correctly hence prints 2.
(2)

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

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

Sandesh H said:   7 years ago
a[0]+1 = &a[0][1] = *(a+0)+1 // all are same and returns address of a[0][1].

since starting address is 1004 i.e &a[0][0]=1002...address of a[0][1]= &a[0][0]+4= 1002+4= 1006.

*(*(a+0)+1 = *(a[0]+1) = *(&a[0][1]) // dereferencing the address(value) i.e a[0][1]=2.

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)

Sowmya said:   2 decades ago
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.

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.

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.