• ### Popular Now

• 13
• 18
• 19
• 27
• 10

#### Archived

This topic is now archived and is closed to further replies.

# How do I pass a 2 dimension array?

This topic is 6114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This has plagued me for some time now, I''ve talked to a lot of University guys (majouring in Computer Science) and I haven''t gotten an answer yet here: void function(int **pointer) { cout< I get this error: error C2664: ''function'' : cannot convert parameter 1 from ''int [1][2]'' to ''int ** '' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I don''t get it, but I don''t see too many multi-dimensional arrays used either

##### Share on other sites
Just cast it. You''re compiler is being overly sensitive.
  function((int **)array);

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
Ok ... the reason for the trouble is this:

An array is not really EXACTLY the same a a non-array pointer ... and a multidimensional array makes this obvious. In C the compiler automatically treats a single dimensional array as a pointer ... because it can, and they decided it was a good idea ... BUT this makes people expect 2 dimensional arrays to work like a pointer-to-pointer .. which it does ... MOSTLY ... but the difference lies in the requirements of the brace operator ... when you index something in an array ... the compiler converts the expression to pointer arithmatic ... and when you use a multidimentional array .. like this

  int myData[8][10];int x = myData[4][2];

the compiler generates an expression like this (much more optimized of course):

int x = *(myData + ((sizeof(int) * (4 * 10)) + (sizeof(int) * 2))

everthing in that expression is just like it would be for a pointer except for ONE thing ... see that magic 10 ... that matches the number of columns in the array ... it is how far apart 2 rows in the array are (times the sizeof the datatype of course). So you see ... the compiler cannot assume two arrays ... say size 3x2 and 6x7 can be passed to the same function .. because they need different math to figure the distance between rows ... so the compiler does NOT cast a multi-dimensional array to it''s point equivelent .. because that would be missing needed data.

Here is how you declare a function to take a multi-dimentional array:

  void OneDim(char data[]);void TwoDim(int array[][10]);void ThreeDim(float fArray[][4][5]);

see how it is always the left-most brace is always empty ... this is because the function will never do any array size checking ... so it doesn''t know or need to know that number.

The top form is prefered for 1 dimensional arrays over using pointers .. because it tells your user you EXPECT an array .. not a single element point .. and arrays must be allocated and deleted differently than normal pointers as well ...

hope that helps.

##### Share on other sites
A bunch of university guys in comp sci can''t answer this question? Geez...

Given

int myArray[10][10];

all of the below pass the same address:

function ( &myArray[0][0] );

function ( myArray[0] );

function ( myArray );

##### Share on other sites
quote:
Original post by bishop_pass
A bunch of university guys in comp sci can''t answer this question? Geez...

My guess is that either: 1 they didn''t understand what he was asking, 2 he didn''t understand what they were telling him to do (possibly caused by number 1), or 3 they''re all VB programmers .

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
quote:
Original post by Xai

the compiler generates an expression like this (much more optimized of course):

int x = *(myData + ((sizeof(int) * (4 * 10)) + (sizeof(int) * 2))

everthing in that expression is just like it would be for a pointer except for ONE thing ... see that magic 10 ... that matches the number of columns in the array ... it is how far apart 2 rows in the array are (times the sizeof the datatype of course). So you see ... the compiler cannot assume two arrays ... say size 3x2 and 6x7 can be passed to the same function .. because they need different math to figure the distance between rows ... so the compiler does NOT cast a multi-dimensional array to it's point equivelent .. because that would be missing needed data.

Ok, I took a look through the C99 Draft and came up with this, which doesn't quite agree with what you're saying. I was looking because I believe that a constantly sized multidimensional array is equivelant to a variable of the same type with a number of "pointer levels" equal to the array's dimensions .
quote:
C99 Draft (Page 71)

EXAMPLE   Consider the array object defined by the declaration
int x[3][5];
Here x is a 3x5 array of int s; more precisely, x is an array of three element objects, each of which is an array of five int s. In the expression x[i] , which is equivalent to (*((x)+(i))) , x is first converted to a pointer to the initial array of five int s. Then i is adjusted according to the type of x , which conceptually entails multiplying i by the size of the object to which the pointer points, namely an array of five int objects. The results are added and indirection is applied to yield an array of five int s. When used in the expression x[i][j] , the array is in turn converted to a pointer to the first of the int s, so x[i][j] yields an int .

That bolding/tabbing wasn't my idea, it was already there, so it doesn't mean the bolded stuff is more important or anything. Anyway, I think that the quote shows that a pointer pointer is sufficient to reference an item within a two dimensional array. This is a constructive debate, I'm not trying to do this as an attack or anything .

[EDIT: The forum warped the code portions. I'm trying to redo it using Latin-Iso HTML thingies, but I don't know if I remember them off the top of my head, and MSDN is having Windows problems]

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on June 22, 2001 3:16:28 AM