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;
}
Discussion:
61 comments Page 6 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.
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.
Habib said:
1 decade ago
Why cant a[0] be 1, it represents the rvalue, not lvalue I guess !
Anonymous said:
1 decade ago
*(a[0]+1):
a[0]+1 = 1002+4 = 1006
The value at 1006 ie, the second position is 2.
a[0]+1 = 1002+4 = 1006
The value at 1006 ie, the second position is 2.
Jithin mathew said:
1 decade ago
Thank you all.
Preethi said:
1 decade ago
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.
*(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.
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.
*(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.
Sivakumaran said:
2 decades ago
a[0]+1= 1002+4=> 1006
*(a[0]+1) = 2
*(*(a+0)+1)=???
*(a[0]+1) = 2
*(*(a+0)+1)=???
Gowthami said:
2 decades ago
Please clear information about problem.
Shree said:
2 decades ago
I think 1006 is the memory location but why can't it be 520.
N Prathyusha said:
2 decades ago
Please clear me the doubt that int is declared 4bytes.
How (*a[0]+1),*(*(a+0)+1) can be 2?
How (*a[0]+1),*(*(a+0)+1) can be 2?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers