[Solved] Is return *array; valid? [closed]


Yes and no. Yes, because it is the way you should return it. No, because you should (and have to) use new[] operator (or malloc function). So basically you should write:

int* function(int n){
 int i = 0;
 int *array = new int[n]; // or c-style malloc(n*sizeof(int))
 for(i = 0; i<=n ; i++){
  array[i] = i;
 }
 return array;
}

Why? Because if you create an array with int array[i] it will ‘disappear’ when function end (it’s all about variable range).

5

solved Is return *array; valid? [closed]