2D array, and passing it to function and returning it back (in C/C++)?

Started by
3 comments, last by NewbieA 20 years, 2 months ago
Hi, anyone know how to play with a 2D array? passing it to a function and back? I'm new to this...

double tmpA[10][10];
double **tmpB;
what should or could i do with them? or should i play with STL instead?
   
double functionA ( double **tmpA  )
{ int i,j;
  for(j=0; j<10; j++)
    for(i=0; i<10; i++)
      tmpA[j][i] = 1;
  return tmpA;
}

double** functionB ( double **tmpB  )
{ int i,j;
  for(j=0; j<10; j++)
    for(i=0; i<10; i++)
      tmpB[j][i] = 1;
  return tmpB;
}
// Did i do this right? somehow i got pass the compiler, but the windows giving me some kind of error...  (@_@)


Thanks for any help~ [edited by - NewbieA on January 25, 2004 10:49:40 AM]
Advertisement
the simplest way, to avoid confusion, is to bind them with a structure.

struct Int10x10
{
int Array[10][10];
};

Int10x10 FunctionA(Int10x10 Array)
{
//......
}


or else,

void FunctionA(int Array[][10])

or

void FunctionA(int Array[10][10])


but I''d rather use a struct wrapper. And I''m not sure how to return them arrays either.

If you use vectors, remember that you loose the convenience of an array, like an array is effectively one continuous block of memory (100 ints stored in one memory space), whereas using vectors or arrays of pointers, you don''t have that advantage (but it can be good for other things).

Everything is better with Metal.

You dont really have to worry about returning arrays because they hold values at specific memory locations.

So if you pass your array to a function, it automatically uses the array's memory address.

double tmpA[10][10];void DoSomething(double tmp[10][10]){    // modify the array.}// pass tmpA to the functionDoSomething(tmpA);  // tmpA is the address of the first element in the array.


So you really never need to return the array you pass to a function. You could return a value (like 0 or 1) to show if the function was successful or not.

-noix-

In this world gone mad, we won't spank the monkey; the monkey will spank us.

[edited by - noixtirdoe on January 25, 2004 3:26:42 PM]

[edited by - noixtirdoe on January 25, 2004 3:27:29 PM]
In this world gone mad, we won't spank the monkey; the monkey will spank us.
quote:Original post by noixtirdoe
You dont really have to worry about returning arrays because they hold values at specific memory locations.

So if you pass your array to a function, it automatically uses the array''s memory address.

double tmpA[10][10];void DoSomething(double tmp[10][10]){    // modify the array.}// pass tmpA to the functionDoSomething(tmpA);  // tmpA is the address of the first element in the array.


So you really never need to return the array you pass to a function. You could return a value (like 0 or 1) to show if the function was successful or not.

-noix-

In this world gone mad, we won''t spank the monkey; the monkey will spank us.

[edited by - noixtirdoe on January 25, 2004 3:26:42 PM]

[edited by - noixtirdoe on January 25, 2004 3:27:29 PM]


If the function resizes the array though, you will need to pass the pointer by reference, so it will point to the new memory location.
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
quote:Original post by oliii
If you use vectors, remember that you loose the convenience of an array,...


How so?

Enigma

This topic is closed to new replies.

Advertisement