C Programming - Pointers - Discussion
Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 6)
6.
Which of the statements is correct about the program?
#include<stdio.h>
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(*(arr))));
return 0;
}
Discussion:
27 comments Page 1 of 3.
Praful said:
1 decade ago
#include<stdio.h>
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(arr+1)));
return 0;
}
Why is answer 4 here?
Ans: 2-D array is nothing but the collection 1-D arrays that are placed after one another.
Here int arr[3][3] = {1, 2, 3, 4};
This array is initialized with only 4 elements, remaining all elements will be initialized to zero.
i.e int arr[3][3] = {1, 2, 3, 4,0,0,0,0,0};
Internally this 2-D array will be treated as:
int arr[3][3] = {{1, 2, 3}, {4,0,0},{0,0,0}}; //three 1-D arrays(0th,1st and 2nd 1-D array).
//3 rows and 3 columns:
1 2 3.
4 0 0.
0 0 0.
*(*(arr+0))points to 1st elements of 0th 1-D array ->1 2 3.
*(*(arr+1))points to 1st elements of 1st 1-D array ->4 0 0.
*(*(arr+2))points to 1st elements of 2nd 1-D array ->0 0 0.
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(arr+1)));
return 0;
}
Why is answer 4 here?
Ans: 2-D array is nothing but the collection 1-D arrays that are placed after one another.
Here int arr[3][3] = {1, 2, 3, 4};
This array is initialized with only 4 elements, remaining all elements will be initialized to zero.
i.e int arr[3][3] = {1, 2, 3, 4,0,0,0,0,0};
Internally this 2-D array will be treated as:
int arr[3][3] = {{1, 2, 3}, {4,0,0},{0,0,0}}; //three 1-D arrays(0th,1st and 2nd 1-D array).
//3 rows and 3 columns:
1 2 3.
4 0 0.
0 0 0.
*(*(arr+0))points to 1st elements of 0th 1-D array ->1 2 3.
*(*(arr+1))points to 1st elements of 1st 1-D array ->4 0 0.
*(*(arr+2))points to 1st elements of 2nd 1-D array ->0 0 0.
(4)
Hassan said:
1 decade ago
@Niki, arr,*arr, arr[0] give same result and arr[0][0] and *(*(arr+i) +j) are same representation in 2d array.
arr[0][0][0] and *(*(*(arr+i)+j)+k) are same representation in 3d array.
So @Niki your representation is invalid in 2d array.
arr[0][0][0] and *(*(*(arr+i)+j)+k) are same representation in 3d array.
So @Niki your representation is invalid in 2d array.
(1)
Rajpati Verma said:
1 decade ago
Array declared are two dimensional but in the we want to print three dimensional value due to this reason type is not match so it thrown error.
Deepak said:
7 years ago
Thanks @Praful.
Coder x said:
8 years ago
We use triple for a 3d array but arr[3][3] is a 2d array.
Uts said:
8 years ago
Thanks @Wikiok.
Sai said:
9 years ago
Thanks @Praful.
Srikanth said:
9 years ago
Now I get it. Thanks to all.
Santhosh said:
9 years ago
Thanks @Praful.
Diya said:
9 years ago
Thanks to all for the clear explanation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers