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;
}
Output: Garbage value
Output: 1
Output: 3
Error: Invalid indirection
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
27 comments Page 1 of 3.

Deepak said:   6 years ago
Thanks @Praful.

Coder x said:   7 years ago
We use triple for a 3d array but arr[3][3] is a 2d array.

Uts said:   7 years ago
Thanks @Wikiok.

Sai said:   7 years ago
Thanks @Praful.

Srikanth said:   8 years ago
Now I get it. Thanks to all.

Santhosh said:   8 years ago
Thanks @Praful.

Diya said:   8 years ago
Thanks to all for the clear explanation.

Praful said:   9 years 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.
(2)

Chaw said:   9 years ago
Why use *** arr?

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.
(1)


Post your comments here:

Your comments will be displayed after verification.