-1

I have the following function:

  int *array1=new int [3];
  void My_function()
  {
  My_set(array1);
  for(int i=0;i<3;i++){    //The output is 5 15 55
  cout<<array1[i]<<endl;
  }
  Display (array1)

  }

  void My_set(int *array1)
  {
  array1[0]=5;
  array1[1]=15;
  array1[2]=55;
   }

  void Display(int *array1){
  for(int i=0;i<3;i++){
  cout<<array1[i]<<endl; //The output is Garbage  -842150451
   }

   }

Note: This problem occurred in complex project but I show my problem in simple code ! THANK YOU :)

1 Answers1

0

You need to declare your array like this int *array1 = new int[3];

Inertiatic
  • 1,270
  • 1
  • 9
  • 12
  • Definitely, I was wondering why he/she was declaring it with new. Figured for testing? Dan brought up a good point though. @user3316887 if you end up creating your array with a dynamic size, make sure to pass the size of the array as an argument to your functions. – Inertiatic Apr 13 '14 at 00:19