Skip to main content
GameDev.net gamedev.net
🔒 Locked

passing a 2d array to a function

Started by RegularKid Jul 14, 2003 at 1:00 AM 9 replies 1k views
Original Post
RegularKid
RegularKid
I feel like an idiot! I can''t seem to pass a 2d array to a function?! Here''s my code (the relevant part anyways):
int myArray[4][4] =
{
    {1, 2, 3, 4},
    (5, 6, 7, 8},
    (4, 3, 2, 1},
    (8, 7, 6, 5},
}

void myFunc(int **array)
{
    int i;
    int j;

    for(i = 0; i < 4; i++)
    {
        for(j = 0; j < 4; j++)
        {
            cout << array[i][j] << endl;
        }
    }
}

int main()
{
    myFunc((int **)myArray);
    return 0;
}
I''ve also tried ''*'' and ''&'' when passing my array into the function, but no luck. Any ideas? This seems so simple to me, but I am just having a brain fart or something! Thanks!
Raloth
Raloth
change

void myFunc(int **array)

to

void myFunc(int** &array)
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
RegularKid
RegularKid
I tried it, but I get this error:


parse error before '&' token


I don't beleive that is correct syntax...thanks, though. Any other thoughts?

[edited by - regularkid on July 14, 2003 2:09:13 AM]
Chris Hare
Chris Hare
int** is a pointer to a pointer, and can be used for a multi-dim dynam array,

int array[4][4] can be accessed via a pointer to the first element (or any other really) since the elements are contiguous in memory

i.e. void f(int* array)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
array[j] = 0;
}

for functions like f that may operate on unknown array-dims, just pass the array-dims as params.

PS myarray is an int*
HTH

Wizza Wuzza?


[edited by - chris hare on July 14, 2003 2:25:54 AM]

[edited by - Chris Hare on July 14, 2003 2:27:26 AM]

[edited by - Chris Hare on July 14, 2003 2:28:41 AM]
Chris Hare
Chris Hare
In f(...) it should be array[ i ][ j ], I'm bad at this post-HTML stuff

Wizza Wuzza?

[edited by - Chris Hare on July 14, 2003 2:30:42 AM]
Fruny
Fruny
your options are


void myFunc( int array[4][4] )
{
int i;
int j;

for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
cout << array[i][j] << endl;
}
}
}


or


void myFunc( int array[][4] )
{
int i;
int j;

for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
cout << array[i][j] << endl;
}
}
}


or


void myFunc( int* array )
{
int i;
int j;

for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
cout << array[i*4+j] << endl;
}
}
}


And nothing else.

A bidimensional array is not an int**.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
RegularKid
RegularKid
if I were to use this option:


void myFunc(int array[][4])
{
...
}


what is the syntax for passing the array into this function? Thanks
HellRaider
HellRaider
quote:
what is the syntax for passing the array into this function? Thanks


For that option (which I think is the most recommended) use:
myFunc( myArray )

Since myArray is of type int*[4] (or int[][4]), it''s fully compatible.
kdogg
kdogg
You would just call

myFunc(array);



I don''t believe you can use myFunc(int *array). array is not an int*, it''s an int(*)[4]. By the way, if you''re just using a static 4x4 array, you could make it a single dimensional array and save yourself some funky syntax.
RegularKid
RegularKid
Cool! That worked great...just one more question (i promise!). Ok, the array that I have (i forgot to mention) is a constant array, ie:


const int array[][4] =
{
...
}


How, would I go about passing this in? I tried casting it, but I don''t know how to cast to the type [][4]? Thanks a lot!
Fruny
Fruny
RegularKid : declare myFunc as
myFunc( const int array[][4] ); or
myFunc( const int array[4][4] ); or
myFunc( const int* array );

Then just pass the array identifier to the function : myFunc( myArray );

quote:
Original post by kdogg
I don't believe you can use myFunc(int *array). array is not an int*, it's an int(*)[4].


C++ multidimensional arrays are row-major, continuous blocks of memory. Accessing them through a pointer has the effect of flattening them and providing access as if it were a single-dimensional array (with array[j][k] becoming array[j*width+k]).

See Stroustrup's "The C++ Programming Language", section C.7 in which the problem ofmultidimensional arrays as parameters is directly addressed.

See also the C++ Standard section 8.3.4 on arrays.

As a final (loosely related) note, in C++, dynamic array is spelled std::vector<sometype>, with multidimensional dynamic arrays being vectors of vectors.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on July 14, 2003 5:30:17 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.