1. ## Returning Array

Hi all,

please correct me if i make a stupid statement....

I know/think i know its not possible to explicitly return an array within C. However i am undert the impression that it is possible to do so using pointers. Ive had a read of various tutorials but none of which really have helped me in what im trying to do.

Im trying to....

1. pass an array to a function,

2. manipulate the array data

3. return the array back to main

thats the simplest way i can describe it i think. I have provided the required sinpets from my main function and the function that i want to edit my array elements.

heres what ive tried......fairly certain its rubbish but what the hell... if some one could help me clear this one up id be very greatful

thanks

Baffa

Code:
```void MixColumns(int *ptr[], int size){

int r[4];

r[0] = ptr[0]*2 + ptr[12] + ptr[8] + ptr[4]*3;

r[1] = 2*(ptr[4]) + ptr[0] + ptr[12] + 3*(ptr[8]);

r[2] = 2*(ptr[8]) + ptr[4] + ptr[0] + 3*(ptr[12]);

r[3] = 2*(ptr[12]) + ptr[8] +ptr[4] +3*(ptr[0]);

ptr[0] = r[0];

ptr[4] = r[1];

ptr[8] = r[2];

ptr[12] = r[3];

return ptr;

}

int main (){

int current_state[16];

int *ptr = current_state;

current_state = MixColumns(ptr, 16);

return 0;
}```

2. When an array is passed as an argument, it decays to a pointer to its first element. Consequently, modifying the elements of the array passed modifies the elements of the array from the caller. In that sense, returning the array would be unnecessary.

3. Remove the line saying "return ptr" and you should be good to go. Of course, you are also not using the size, so that's not needed - it's a good thing to pass the size, but it makes no sense to pass it if all you do is ignore it.

--
Mats

4. you what? so i dont need to return it anyway? you saying to just use a void function or somthing because when the array data is modified in the function?

Oh poo ive realised i put void instead of having

int MixColumns(int ptr[], int size){......

5. Yes, your code, as it stands, updates the info inside the array.

--
Mats

6. Code:
``` line 57: error: expression must have arithmetic or enum type

r[0] = ptr[0]*2 + ptr[12] + ptr[8] + ptr[4]*3;

^```
im getting a lot of these errors throughout the code on compliation.

Is there somthing im doing wrong to manipulate the array elements?

7. Originally Posted by baffa
Code:
``` line 57: error: expression must have arithmetic or enum type

r[0] = ptr[0]*2 + ptr[12] + ptr[8] + ptr[4]*3;

^```
im getting a lot of these errors throughout the code on compliation.

Is there somthing im doing wrong to manipulate the array elements?
Unless this is a 2-dimensional array you're passing, change the function to:
Code:
`void MixColumns(int *ptr, int size){`

8. You are passing an array of pointers. You probably want to change "int *ptr[]" to "int ptr[]".

9. woow that was awesome.....i only get this now any ideas? i dont what an lvalue is?...

Code:
```error: expression must be a modifiable lvalue

current_state = MixColumns(ptr);```

10. MixColumns() is now declared as returning void, right? As such, you cannot assign its return value to current_state is there is no return value.

11. ye but what i wanted to do was then return the data in the mixcolumns function to the current_state column. sorry thats my fault for being a bit vague. I did wonder how having a void function would do that :P

ye i want to assign the array elements in the mixcolumns function to the current state array in main. How would i do that?

12. ye i want to assign the array elements in the mixcolumns function to the current state array in main. How would i do that?
What is the ptr variable for? It looks like you want to write:
Code:
`MixColumns(current_state);`

13. I used ptr as the pointer to the array as I thought that i needed to use that to reference the array and return it.

can there be a case where i can have this in my main function?
Code:
`current_state = MixColumns(current_state);`
thats is ideally what i want but i dont know how i can return the amended array to update current_state.

14. can there be a case where i can have this in my main function?
Yes, but as stated you do not need to return from MixColumns().

15. Originally Posted by laserlight
When an array is passed as an argument, it decays to a pointer to its first element. Consequently, modifying the elements of the array passed modifies the elements of the array from the caller. In that sense, returning the array would be unnecessary.
I agree (of course) and I like the word "decay"! That's exactly what happens.
It should be pointed out (I'm sure it's in the FAQ) that it only happens to the first dimension of a multi-dimensional array. The other dimensional information is useable in the callee at compile-time. For example, the following works no matter what the size of the extra dimensions.
Code:
```int addem3d(int a[][2][2], int size) {
int sum = 0;
for (int i = 0; i < size; ++i)
for (int j = 0; j < sizeof(*a)/sizeof(**a); ++j)
for (int k = 0; k < sizeof(**a)/sizeof(***a); ++k)
sum += a[i][j][k];
return sum;
}```