Two dimensional array

Nov 13, 2010 at 5:52pm
I have a simple C program as below :

int matrix[3][3]=
{
{11,12,13},
{21,22,23},
{31,32,33}
};

int main()
{
printf("Element [1,2] is %d \n" , matrix[1,2]);
return 0;
}

It prints a address of element matrix[0,2].

When i printed the address of matrix[1,0] and matrix[1,1] ... they print the address of matrix[0,0] and matrix[0,1] respectively .. It is the same case with 3rd row as well.

My question why the element of 2nd and 3rd row are showing the addresses of respective elements of row 1 ?
Last edited on Nov 13, 2010 at 5:53pm
Nov 13, 2010 at 6:01pm
matrix[1,2] is the same as matrix[2]
To access an nD array, you need to call n times the subscript operator eg: matrix[1][2]
Nov 13, 2010 at 6:05pm
1
2
3
4
5
6
7
8
9
10
11
12
int matrix[3][3]=
{
{11,12,13},
{21,22,23},
{31,32,33}
};

int main()
{
printf("Element [1,2] is %d \n" , matrix[1][2]); // <- Small change
return 0;
}


EDIT: Bazzy was faster ;)

Last edited on Nov 13, 2010 at 6:06pm
Topic archived. No new replies allowed.