Array, pointer return types of function
Dec 11, 2013 at 9:16am UTC
Please check the following function, is it right?
1 2 3 4 5 6 7 8 9 10 11
unsigned int * mul_arr(unsigned int arr_1[4], unsigned int arr_2[4])
{
int i;
unsigned int result_arr[4];
for (i=0; i<4; i++)
{
result_arr[i] = arr_1[i]*arr_2[i];
}
return result_arr;
}
I want to return Array (result_arr) from function
Last edited on Dec 11, 2013 at 9:18am UTC
Dec 11, 2013 at 9:28am UTC
you can't return a local variable from function.
After the function call the stack of the function will unwind, any local variables will not be pointing to valid location. You have to allocate memory to it
1 2 3 4 5 6 7 8 9
unsigned int * mul_arr(unsigned int arr_1[4], unsigned int arr_2[4])
{
unsigned int *result_arr = new unsigned int [4];
for (i=0; i<4; i++)
{
result_arr[i] = arr_1[i]*arr_2[i];
}
return result_arr;
}
Topic archived. No new replies allowed.