multidimensional arrays

I'm just wondering about two dimensional arrays I'm on chapter 14 (Dynamic Memory Allocation) of Alex Allains book and we are learning about multidimensional arrays and pointer arithmetic

one thing I don't get is why do you have to ONLY include the second size of the array and not the first for example

1
2
3
4
5
6
7
  
   void PrintArray(int array[][4){

    // do something 

}


and if that's possible how come you just can't declare an array in the first place with only the second size mentioned for example

1
2

  int ray[][4];


if anyone could explain this in layman's terms(if possible) that would be great

thanks,
When you declare a 2d array, the compiler needs to know both dimensions*. 2d arrays are actually stored as 1d arrays using the formula for a position: pos = row * NumCols + col, where row and col are the 2d position.

When an array is sent to a function, the compiler knows the size of it, so only needs the NumCols in order to implement the formula above.

* Unless you initialise it directly, in which case the compiler can count them.
Last edited on
Hi,
arr[] == *arr
.
arr[][5] == *arr[5]
.
arr[][][8] == **arr[8]
.
arr[][][][9] == ***arr[9]

etc
Does that help you? :)
thanks guys,it's strating to make sense still a pretty hard subject to get the head around

@closedaccount how come you use it with a pointer? or why do you use the asterix when declaring an array?
Last edited on
closed account (48T7M4Gy)
.
Last edited on
Why don't you skip this chapter in the book. Pointers and dynamic arrays are a thing of the past. In modern C++ we have the STL which makes things much easier.
closed account 5a8Ym39o6 wrote:
arr[][][8] == **arr[8]
.
arr[][][][9] == ***arr[9]

etc

Er, that's wrong. arr[][]... is invalid.
I already knew that long ago :)
Heh, yeah. It's unusual for me to be the one pointing out the error instead of having made the error...
Topic archived. No new replies allowed.