Loading
submit to reddit
May 24, 2013, 06:02:20 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome!
 
   Forum Home   Help Search Forum Login Register  
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: Dynamic Array Allocation  (Read 253 times)
Fred
Newbie
*
Posts: 1


« on: July 08, 2012, 07:37:43 PM »

Hi

Thanks for the great tutorial videos. I have a question about video 36: The following is from the video.
 
********************************************************
int ArraySize;
    cout<<"How many integer should be stored"<< endl;
    cin>>ArraySize;
    int *ipIntArray =new int[ArraySize];
    for (int i=0;i<ArraySize; ++i){
        cout<< "input the integer number "<<i+1<<" :"<<endl;
        cin>> ipIntArray;
    }
    for (int i=0;i<ArraySize; ++i){
        cout<<ipIntArray<<" ";
    }

    cout<<endl;
    delete [] ipIntArray;
***********************************************


 My question is in the loop, when allocating the array elements, why shouldn't we use the dereference operator, as we do for integers? I was expecting to have something like
cin>> *ipIntArray;
and
cout<<*ipIntArray<<" ";

Regards
Fred
Logged
Michael Hall
Administrator
Hero Member
*****
Posts: 903



« Reply #1 on: July 09, 2012, 05:17:16 PM »

That's not quite right. The line
Code:
cin >> ipIntArray;

should be
Code:
cin >> ipIntArray[i];

The bracket and integer value, i, effectively "dereference" the ith element of the array. Use * when you have one element and the brackets when you have an array.

Mike

P.S. You should also use the bracket operator in your later output loop.
Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!