Jump to content
  • Advertisement

Archived

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

shizik

quick 2-dimensional array question in C

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to pass a 2-dimentional array to a function "by reference" or "by ptr" what is the correct way to do this i have: //----------------------- void func(int Arr[][]) int main() { int Arr[20][20]; func(Arr); } //---------------------- what goes into prototype arg list? thanks

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by shizik
i want to pass a 2-dimentional array
to a function "by reference" or "by ptr"

what is the correct way to do this

i have:
//-----------------------
void func(int Arr[][])

int main()
{
int Arr[20][20];
func(Arr);
}
//----------------------

what goes into prototype arg list?

thanks


Well, if you want to pass it by pointer, then,

func(Arr); works just fine because all Arr is is a pointer to a 2D array.

If you want to pass by ref, then in the Prototype, you should have

void func( int &Arr[][]);

-Jason

Share this post


Link to post
Share on other sites
quote:
Original post by scubabbl
If you want to pass by ref, then in the Prototype, you should have

void func( int &Arr[][]);




No, that won't work (besides, AFAIK there are no references in C). You have to specify all array dimensions except the first in the prototype:

void func(int Arr[][20]);

Arrays are always passed "by pointer" in C and C++. If the dimensions are unknown, you should use a pointer argument and pass the dimensions seperately:

void func(int *Arr, int x, int y);



Edited by - spock on November 18, 2001 11:15:19 PM

Share this post


Link to post
Share on other sites
quote:
Original post by spock
void func(int *Arr, int x, int y);

Shouldn''t that be
void func(int **Arr, int x, int y); 

?

Share this post


Link to post
Share on other sites
Does that really matter, Oluseyi? I mean isn't a pointer to a pointer to the first element of the array the same as just the pointer to the first element of the array? That's not very well-worded I know, but you know what I mean?
If you have a 2-dimensional array of 3 times 4 integers then wouldn't that basically be just a one-dimensional array of 12 integers?

EDIT: Okay, I just realized that a pointer to a pointer is NOT the same as just a pointer, but still, I do think he could just pass a pointer to the first element of the array along with the dimension of the array, and then do whatever he wants to do with the array in his function.

Edited by - Red Ant on November 19, 2001 5:59:58 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Red Ant
EDIT: Okay, I just realized that a pointer to a pointer is NOT the same as just a pointer, but still, I do think he could just pass a pointer to the first element of the array along with the dimension of the array, and then do whatever he wants to do with the array in his function.

Well, it''s just that you might have problems dereferencing a pointer twice. As far as the function is concerned, it is only obtaining a pointer (which can only be deref''d once), so to be able to do Arr[x][y] you''d have to cast from a pointer to a double pointer, which may not be permitted and so on and so forth.

Then when you''re tired of battling the compiler, you''ll make it a double pointer and get on with life.

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Shouldn''t that be
void func(int **Arr, int x, int y);  

?


That might be a convenient (syntax-wise) way of doing it, but with that declaration you''re not really passing a 2-dimensional array of ints anymore, are you?

Share this post


Link to post
Share on other sites
quote:
Original post by spock
That might be a convenient (syntax-wise) way of doing it, but with that declaration you''re not really passing a 2-dimensional array of ints anymore, are you?

*sigh...*
  
#include <iostream>

using namespace std;
void function(int *arr, int x, int y)
{
for(int i = 0; i < x; ++i)
{
for(int j = 0; j < y; ++j)
arr[i][j] = i * j;
}
}

void display(int *arr, int x, int y)
{
for(int i = 0; i < x; ++i)
{
for(int j = 0; j < y; ++j)
cout << arr[i][j] << endl;
}
}

int main(int argc, char *argv[])
{
int arr[10][12];
function(arr, 10, 12);
display(arr, 10, 12);
return 0;
}



--------------------Configuration: arr_ptr - Win32 Debug--------------------
Compiling...
arr_ptr.cpp
d:\microsoft visual studio\arr_ptr.cpp(9) : error C2109:
subscript requires array or pointer type
d:\microsoft visual studio\arr_ptr.cpp(9) : error C2106:
''='' : left operand must be l-value
d:\microsoft visual studio\arr_ptr.cpp(18) : error C2109:
subscript requires array or pointer type
d:\microsoft visual studio\arr_ptr.cpp(25) : error C2664:
''function'' : cannot convert parameter 1 from ''int [10][12]'' to ''int *''
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
d:\microsoft visual studio\arr_ptr.cpp(26) : error C2664:
''display'' : cannot convert parameter 1 from ''int [10][12]'' to ''int *''
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
Error executing cl.exe.

arr_ptr.exe - 5 error(s), 0 warning(s)

Are we done?

Share this post


Link to post
Share on other sites
Oluseyi, I understand what it is you are trying to prove - it''s just that it''s totally beside the point. When more than one array dimension is unknown you can''t access the elements through the [][] notation anymore, because this requires knowledge of the element layout that the compiler simply doesn''t have.

For the benefit of shizik and other readers, here''s how you might define func():


void func(int *arr, int x, int y)
{
for(int i = 0; i < x; ++i)
{
for(int j = 0; j < y; ++j)
arr[i * y + j] = i * j;
}
}


Oh, and when using C++ (as opposed to C) there''s little reason for messing around with multidimensional arrays like this - use std::vector instead.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!