C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 18)
18.
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>
int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
return 0;
}
Discussion:
54 comments Page 1 of 6.
Rishab said:
4 years ago
@All.
Suppose if you have an array a[10]
And you want to give a[5] = 12.
You can write *(a+i) = 12 (this is nothing but a pointer way of using array)
if you print(a) it will point to the base address of the array.
these are the basics of array.
Now let's work with the question.
if we have 3d array a[10][10][10].
and you want to make a[5][6][7] = 30,
You can use *(*(*(a+5)+6)+7) = 30,
count the stars needed to give the value. 3 stars,
so until 3 stars you will be printing the address
in this case.
"a"
will print address of a
"*a"
will print address of *(a+0) which is same as a
"**a"
will print address of *(*(a+0)+0) which is same as a
but
"***a"
will print VALUE of *(*(*(a+0)+0)+0) which is 1
Hope you all get it.
Suppose if you have an array a[10]
And you want to give a[5] = 12.
You can write *(a+i) = 12 (this is nothing but a pointer way of using array)
if you print(a) it will point to the base address of the array.
these are the basics of array.
Now let's work with the question.
if we have 3d array a[10][10][10].
and you want to make a[5][6][7] = 30,
You can use *(*(*(a+5)+6)+7) = 30,
count the stars needed to give the value. 3 stars,
so until 3 stars you will be printing the address
in this case.
"a"
will print address of a
"*a"
will print address of *(a+0) which is same as a
"**a"
will print address of *(*(a+0)+0) which is same as a
but
"***a"
will print VALUE of *(*(*(a+0)+0)+0) which is 1
Hope you all get it.
(11)
Pushparj Manikam said:
5 years ago
a,*a,**a is base address.
***a is return the value of base address.
***a is return the value of base address.
Ajitesh said:
6 years ago
A gives the address of 3d aray.
3d array a[blocks][rows][columns].
*a gives the address of 1st block (z direction) of the array which is same as the address of a.
**a gives the address of 1st row of 1st block of the array which is same as the address of the 1st block.
***a gives value at address specified by **a.
3d array a[blocks][rows][columns].
*a gives the address of 1st block (z direction) of the array which is same as the address of a.
**a gives the address of 1st row of 1st block of the array which is same as the address of the 1st block.
***a gives value at address specified by **a.
Anjaliraj said:
6 years ago
Thanks for the answer @Sriram.
Pattu said:
7 years ago
Thanks for the answer @Shriram.
Muhammad Fouad said:
7 years ago
Hey guys, just want to figure out some concepts:-
The array name (without a subscript) is a pointer to the first element in the array.
So, when saying printf( "%d, %d, %d and %d", a, *a, **a, ***a );
The first a is a pointer to the first element, i.e returns the address of the array or the first element of the array, the second a is a pointer to a pointer, also returns am address, the third one also the same, but the last one is exceptional, because the compiler already knows that a 3D array was declared, so a maximum of triple dereferencing statements is obtained, because the forth a which is ***a will return the actual value which is 1!!.
Hope you got it.
The array name (without a subscript) is a pointer to the first element in the array.
So, when saying printf( "%d, %d, %d and %d", a, *a, **a, ***a );
The first a is a pointer to the first element, i.e returns the address of the array or the first element of the array, the second a is a pointer to a pointer, also returns am address, the third one also the same, but the last one is exceptional, because the compiler already knows that a 3D array was declared, so a maximum of triple dereferencing statements is obtained, because the forth a which is ***a will return the actual value which is 1!!.
Hope you got it.
Rohan said:
7 years ago
@Ashu, @Payal.
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
/* It specifies %d that's why the data stored in ***a is printed instead of its address.... */.
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
/* It specifies %d that's why the data stored in ***a is printed instead of its address.... */.
Payal said:
8 years ago
Why ***a=1?
Please explain it.
Please explain it.
Ashu said:
8 years ago
Why ***a=1? Please explain it.
Shilpa said:
8 years ago
Well done @Sangaraj.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers