Array not returned correctly

Posted by hp1 on Stack Overflow See other posts from Stack Overflow or by hp1
Published on 2010-04-15T15:36:42Z Indexed on 2010/04/15 16:13 UTC
Read the original article Hit count: 164

Filed under:
|

I am trying to return a simple array, but I am not getting the correct result. I am getting the following

arr1[0] = 1 
arr1[1] = 32767

result while the result should have been

  arr1[0] = 1 
  arr1[1] = 15

Please suggest.

int *sum(int a, int b){
  int arr[2];
  int *a1;
  int result = a+b;
  arr[0]= 1;
  arr[1]= result;
  a1 = arr;
  return a1;
}
int main(){  

  int *arr1 =  sum(5,10);
  cout<<"arr1[0] = "<<arr1[0]<<endl;
  cout<<"arr1[1] = "<<arr1[1]<<endl;

  return 0;

}

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays