Advertisement

sending a 2d Array to a funktion? help

Started by February 14, 2003 02:10 AM
10 comments, last by kappa the imp 21 years, 9 months ago
I need to send a 2d Array to a funktion andI have no clue how, could somebody help me?
void myfunction(array[size][])
It''s funky but it works

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

Advertisement
you can do this

example:

void funcx(int *array)
{
int f,j;
for(j=0;j<=100;j++)
f = array[1][j]; //you can access the array like this
}

int a[100][100];

main()
{
funcx(a); //passing array to function
return 0;
}

[edited by - lone_ranger on February 14, 2003 4:15:32 AM]

[edited by - lone_ranger on February 14, 2003 4:16:28 AM]
Virtus junxit, mors non separabit
It kind of depends on your language of choice. If you''re using Java, for instance, the array itself know how big it is - and you could do the following:


  public void arrayFunction( int[] theArray ){  for( int i = 0; i < theArray.length; i++ )  {    System.out.println( "theArray[i]: " + theArray[i] );  {}  


But if you are using C or C++ you need to pass along the size of the array too - like this:


  void arrayFunction( int * theArray, int arraySize ){  int i;  for( i = 0; i < arraySize; i++ )  {    printf( "theArray[i]: %i", theArray[i] );  }}  


-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
the function''s parameters should be like so:

rtype function(type array[][numOfCols], int size);

this may work too:

rtype function(type **array, int rows, int cols);

hope that helps.
You can also send the function a single, very long array. For example if you want an array of size [200][200] you would declare it like int myArray[200*200]. You could then access it like so:


  for (int j = 0; j < 200; j++){   for (int i = 0; i < 200; i++)   {      cout<<myArray[i+200*j];   }}  


Andre LaMothe does this in his TOTWGPG book (though not for the same reason; he does it to access the memory... or something :D)

Peon
Peon
Advertisement
void function( int **array ){}?

.lick
WOW, I entirely missed the fact that it was a 2d array.....my bad....

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
I had to use this when I calloc''ed a multidimensional array...


  for (int j = 0; j < 200; j++){   for (int i = 0; i < 200; i++)   {      cout<<myArray[i+200*j];   }}  


Am I to assume I was correct in this usage?
quote: Original post by Peon
You can also send the function a single, very long array. For example if you want an array of size [200][200] you would declare it like int myArray[200*200]. You could then access it like so:

Andre LaMothe does this in his TOTWGPG book (though not for the same reason; he does it to access the memory... or something :D)

Peon


This would simply make the array 1D but the same size as the 2D. LaMothe does this when accessing a DirectDraw videobuffer because DirectDraw guarantees a linear videobuffer. Hence, you need to access it as if it were a 1D array.

[edited by - Arkainium on February 14, 2003 2:16:57 PM]

This topic is closed to new replies.

Advertisement