Array, pointer return types of function

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