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 2 of 3.
Manju said:
1 decade ago
Array declared as two dimensional but in the printf statement there is three dimensional pointer notation so complier thrown error.
@Singh said:
1 decade ago
9 element should be present their is only 4 other value can be assign as 0 or garbage value.
Farzana Shaik said:
1 decade ago
We use triple pointer for 3-Dimensional array only.
But in above program 2-dimensional array is given.
But in above program 2-dimensional array is given.
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.
Lavanya 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.
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(arr+1)));
return 0;
}
Why is answer 4 here.
Rushit said:
1 decade ago
@Lavaanya.
*(arr+1) is one unit here it shifts 3(cause 3 row).
So. it will point to next column.
You can access any element via this = *(*(arr+column)+row).
*(arr+1) is one unit here it shifts 3(cause 3 row).
So. it will point to next column.
You can access any element via this = *(*(arr+column)+row).
Niki said:
1 decade ago
Can we take it like this?
*(*(*(arr))=*(*(*arr[0][0]))=*(*(1))
Some one please explain me.
*(*(*(arr))=*(*(*arr[0][0]))=*(*(1))
Some one please explain me.
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)
Chaw said:
1 decade ago
Why use *** arr?
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers