Hi met with an array question while studying , here it goes .
we declare: int x[4][3]; then x[i][j] is also said to be :
1)*(x[i] + j)
2)*(&x[0][0] + 4*i + j)
3)Both (a) and (b)
4)None of the above
actually i thing the answer shld be 1 or 4 .
This is a discussion on HELP ! Array Query . within the C Programming forums, part of the General Programming Boards category; Hi met with an array question while studying , here it goes . we declare: int x[4][3]; then x[i][j] is ...
Hi met with an array question while studying , here it goes .
we declare: int x[4][3]; then x[i][j] is also said to be :
1)*(x[i] + j)
2)*(&x[0][0] + 4*i + j)
3)Both (a) and (b)
4)None of the above
actually i thing the answer shld be 1 or 4 .
If you have no real idea how the compiler handles these expression, why don't you try a little testprogram and let the compiler do the work for you :-)
Though, answer 1) is correct!Code:#include <stdio.h> int main () { int x[4][3]; printf("%p -- %p\n", &x[2][1], &(*(x[2] + 1))); return 0; }
- Andi -
1)*(x[i] + j)
In this case x[i] is an adress. Incrementing that addres by 'j' and dereferencing it all with * infront gives you the value that x[i][j] has.
1 2 5
5 7 9
2 8 3
1 8 9
Thats the declared array - 4 rows, 3 columns with random numbers.
x[0] = 1
x[1] = 5 //first element of the row
*(x[1] + 1) = 7 //first element of the row + 1 adress forward