# Returning arrays from a function

• 12-13-2001
mattbrrtt
Returning arrays from a function
I am trying to return a pointer address to my main function but it isn't working. I am using Windows 98, Visual Studio 6.0 and making my program for a win 32 console application. I have put the function that I am working on below. The purpose of this program is to complete an assignment for school. What this function is to do is to take a complex array of predefined numbers and square them, storing the result in the original array. I am pretty sure that I have messed this up badly.

I am using pointers because I am under the impression that if you just pass the array the contents are protected, and would not be changed when the function returns. Is this true?

The instruction and the book that in the course that I am taking has not been the best. Does anyone know where I can find a good explanation of using pointers, functions and structs?

int square_function(int * original[][COLUMNS], int rows)
{
int i, j;
for (i=0 ; i < rows ; i++)
for (j=0 ; j < COLUMNS ; j++)
*original[i][j] = (*original[i][j] * 2);

return (*original);
}

Thank you for any help or direction.
• 12-13-2001
Garfield
I think you have the right idea. You can't really return an array. You return a pointer to the first element of the array and then...poof...read 'til the cows come home :D

--Garfield
• 12-13-2001
quzah
> int square_function(

You'll need to change this to return a pointer.

int *returnArray( int array[] ) { return array; }

Quzah.
• 12-13-2001
mattbrrtt
I understand what Garfield has posted. It makes sense and I think that I have a similar example. I didn't recognize the problem that I have, and what all of the possible solutions are, given the explanantions in class.

Seeing Quzah's message leads me to this question.

For this line of code to prototype the function:

int square_function(int * original[][COLUMNS]);

Would this be the replacement to return the pointer?

int *square_function( int * original[] ) { square_function; };

To call this function in the code is this the correct format:

square_function( original ) {square_function};

Thanks again. The answers that I am getting here are far superior to what I get in class.
• 12-14-2001
Stoned_Coder
There is no need to return anything because arrays are passed by reference so any changes you make inside the array will still be there when you return to the calling function. Secondly when you square something that is not the same as multiplying by 2.
• 12-14-2001
Garfield
Yes, you are right. When you specify something like this:
Code:

```char array[5]; sscanf(line, "%s", array);```
You don't need to do "&array" because that is already the address of array. If you were to do "&array", then that is two levels of indirection.

--Garfield
• 12-14-2001
swoopy
void square_function(int original[][COLUMNS]);

>Would this be the replacement to return the pointer?

Code:

```void square_function( int original[][COLUMNS] ) {   int i, j;   for (i=0 ; i < ROWS ; i++)       for (j=0 ; j < COLUMNS ; j++)         original[i][j] *= 2; }```
>To call this function in the code is this the correct format:
Code:

```const int ROWS = 5; const int COLUMNS = 5; int main(void) {   int r,c;   for (r=0; r<ROWS; r++)       for (c=0; c<COLUMNS; c++)         original[r][c] = 2;   square_function(original);   for (r=0; r<ROWS; r++)       for (c=0; c<COLUMNS; c++)         printf("original[%d][%d]=%d\n",r,c,original[r][c]);   return 0; }```
• 12-14-2001
swoopy
And if you want to square them, it seems like instead of this:
original[i][j] *= 2;

You would want this:
original[i][j] *= original[i][j];