Qazazaz
Qazazaz

Reputation: 120

Why the array elements are having different values inside and outside the loop?

While doing some code,I am not having the expected results due to self change in array values inside and outside the loop.why its happening?

char* arr[2];
int i=0;
char inp[20];
while(i<2)
    {
    cin>>inp;
    arr[i]=inp;
    cout<<arr[i]<<endl;
    i++;
    }
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;

For the input :

aaa
bbb

The output is:

aaa
bbb
bbb
bbb

Upvotes: 0

Views: 341

Answers (1)

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33046

The following line doesn't do what you think:

    arr[i]=inp;

Here you are assigning to arr[i] a pointer to inp, not copying the input buffer into arr[i]. During the same iteration (i.e. at the first cout << arr[i]) everything is fine. As soon as you overwrite the contents of inp, the changes will reflect in all the cells of arr you have assigned to that same pointer. In fact, at the end of the loop all cells of arr point to inp, which means you will always see the last value extracted from cin.

As a solution, make arr an array of "strings" and copy the input buffer using strncpy:

char arr[2][20];
int i=0;
char inp[20];
while(i<2)
    {
    cin>>inp;
    strncpy(arr[i], inp, 20);
    cout<<arr[i]<<endl;
    i++;
    }
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;

Or use C++ std::string if possible.

Upvotes: 3

Related Questions