0

How does the computer know when one row ends and another row begins in a 2d array? I have

int[2][2] = {{0, 1}, {2, 3}} 

which saves into contiguous memory the values of 0, 1, 2, and 3. int[4] = {0, 1, 2, 3} would save into memory the same values. How does the computer know to return a 2 for int[1][0]. It would need to know the length of a column so that it can multiply 1*(column length of 2) + 0 to get the third element. I'm programming in cpp. Thanks.

quantdev
  • 23,517
  • 5
  • 55
  • 88
rgkirch
  • 35
  • 1
  • 7
  • The **compiler** knows size information about all arrays, regardless of whether they're 2D or not. Read [the answer to this question](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c), it explains everything there is to know about arrays. I'm going to close your question as a duplicate of that question. If you feel it doesn't answer your question, ping me by using `@Praetorian` in your reply. – Praetorian Jun 02 '14 at 02:58
  • Thank you. I'm still working through that set of answers; it's a lot to digest. When I searched I didn't find any explanations that good so thank you for referring me to there. I'll let you know if anything is still unclear after that. Again, thank you. – rgkirch Jun 05 '14 at 19:08
  • Glad to be of help :) Now when someone tells you *C arrays are just pointers* you can smack them over the head, and explain why they're wrong :) – Praetorian Jun 05 '14 at 19:11

1 Answers1

2

You told it where one row ends,

int[2][2] = {{0, 1}, {2, 3}} 
       ^
   right here

Did you expect it to forget?

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274