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.

PUSHPARAJ said:   5 years ago
*(a[0]+1) is Nothing. Just a[0][1] value is 2.

*(*(a+0)+1)) as same as a[0][1] value is 2.
(4)

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

Sakshi munya said:   6 years ago
Thanks @Shahizzz Rulezzz.

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.

Prakash said:   7 years ago
*(*(a+0)+1)=*(a[0]+1)=*(1000+1)=*(1004)=2.

Means: *(a+0)= a[0] and *(1004) bcz datatype is inetger so pointer always incremented by 4.

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)

Anomis said:   7 years ago
Why answer is 1006, 2, 2?

Nagu said:   7 years ago
The base address is (int a=448).i using integer then i increment (a+1) the base address is 452.
integer 4 byte in linux.
integer 2 byte in Turbo c.

Himanshu said:   7 years ago
How can anyone know the base address?

It can be 448 or another address as well.

Harshal Shirude said:   8 years ago
a[0]+1= 1002+4=> 1006.
*(a[0]+1) = 2.
*(*(a+0)+1)=> *(*((1002+0)+1)) => **(1004) => 2 = a[1].


Post your comments here:

Your comments will be displayed after verification.