Archived

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

NewbieA

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

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 function

DoSomething(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]

Share this post


Link to post
Share on other sites
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 function

DoSomething(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.

Share this post


Link to post
Share on other sites