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 4 of 7.
Sakshi munya said:
6 years ago
Thanks @Shahizzz Rulezzz.
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?
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.
*(*(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.
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.
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.
Jithin mathew said:
1 decade ago
Thank you all.
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.
Habib said:
1 decade ago
Why cant a[0] be 1, it represents the rvalue, not lvalue I guess !
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers