explain this concept

,

int arr[3][2]={{1,2},{3,4},{5,6}};

printf("%d %d %d",arr[0][3],arr[1][2],arr[2][0]);

can anyone explain how can we access elements in array using notations used in printf

1 Like

it should give RTE, imho

answer is 4 5 5 can anyone explain

I don’t know since collage my first programming we suffer from this syntax “Printf()”, so instead we use “Println()” and some times “Print()” both better and easy.

as far as i understand it goes like this…

any matrix is stored in linear form in the memory…so the matrix…

1 2
3 4
5 6

is stored in the form…

1 2 3 4 5 6

now when u access arr[0][3]…the 0 points to the 0th row…i.e. starting at 1…and 3 moves the pointer to index 3 of the array starting from 1 i.e. 4…hence 1st number that is printed is 4…the next access is arr[1][2]…1 points to starting of the 1st row…i.e. starting at 3…and 2 moves the pointer to index 2 of the array starting from 3…i.e. 5…hence 2nd op is 5…and the last 1 is obviously 5…hope this helps…:slight_smile:

EDIT

also i have come across this many times that even if the array index go out of bounds in c or cpp…the codes run without an RE…as it mostly does not check for bounds of the array…it just prints the value at the pos…[&arr+index]…only when an unallocated memory unit is accessed does it give an RE…LINK1LINK2…hope this helps…:slight_smile:

6 Likes

@kunal361 has explained most of the part. I am assuming that you have understood “how the array is stored” part. Let me tell you few more things, that will clear all your doubts.
Lets consider example of 1-dimensional array.

#include<stdio.h>
int main()
{
 int a[]={1,2,3,4,5};
 for(int i=0;i<5;i++)
 printf("%d\n",a[i]);
}

Focus on printf statement. We are using “a[i]” to access ith element of array a. Compiler will convert a[i] to something like this *(a+i). Try replacing a[i] with *(a+i), you will get same output. ((a+i) is address, and *(a+i) gives value at address (a+i)).

Now, lets see the amazing part. If what I wrote above is really true, then we can say *(a+i) is also *(i+a). (Commutative property of addition). Which means, we can write i[a] in place of a[i], and still get the same output. Confirm it yourself.

#include<stdio.h>
int main()
{
 int a[]={1,2,3,4,5};
 for(int i=0;i<5;i++)
 printf("%d\n",i[a]);
}

Now the next part. 2-dimensional array. Lets take your example. Try compiling this. I have removed bounds on the array.

#include<stdio.h>
int main()
{
	int arr[][]={{1,2},{3,4},{5,6}};
	printf("%d %d %d",arr[0][3],arr[1][2],arr[2][0]);
}

You will get 1 error, “[Error] declaration of ‘arr’ as multidimensional array must have bounds for all dimensions except the first”
Now, lets change our code to this.

#include<stdio.h>
int main()
{
	int arr[][2]={{1,2},{3,4},{5,6}};
	printf("%d %d %d",arr[0][3],arr[1][2],arr[2][0]);
}

Now, it compiles fine.

If you understood the 1-dimensional case, you can think of why this happens. Compiler needs to convert arr[0][3], and other such references to address locations, and that’s why it requires all the bounds except the first one. Here, arr[0][3] is converted to *(arr+(0*2)+3), i.e. *(arr+3). Obviously, 4 is stored at that location. Similarly, arr[1][2] is *(arr+(1*2)+2), i.e. *(arr+4). 5 is stored at this location. In these conversions, we required bound on second dimension, i.e. 2. And, bound on 1st dimension was never required.

If you want me to explain anything else, please comment.

1 Like

I want to know what actually happens.

how does it give an answer at all, i think it should return error

It is correct output you can check by compiling .

i think i have explained that…can u pls be more specific with ur ques!!!

I think @kunal361 has already explained the output @yella venkatesh.
And the program works fine @garakchy. See here: YTnHiF - Online C Compiler & Debugging Tool - Ideone.com

1 Like

Thanks for your answer .Now, I clearly understood what compiler does.

1 Like