3

Here is the code::

#include <iostream>
using namespace std;

const int MAX = 4;

int main ()
{
  char key[20];
  char *names[MAX];

  for (int i=0; i<MAX; i++)
  {
    cout << " entr keys\n";
    cin >> key;
    names[i]=key;
    cout<< names[i];
  }
  for(int i=0; i<MAX;i++)
  {  
    cout << names[i];
  }
  return 0;
}

When I enter the keys and print them in the 1st for loop they show the right value, but when I print names[i] in the 2nd for loop it keeps showing the last key entered again and again.

Please tell me: where am I going wrong?

2
  • 3
    without using string Your code shows exactly why you should be using string, you seem to have no idea how raw arrays work and how to copy data around. Hint: this names[i]=key; does not copy what key points to, it merely copies the key pointer itself. Commented Sep 8, 2014 at 6:20
  • 1
    You need enough space for 4 strings like key, and you need to make sure the names elements point to those, rather than always pointing to key. And you need to use strcpy() or memmove() to copy the key into the other strings. Or, better, use <string> and string so you don't have to worry about it in future. Commented Sep 8, 2014 at 6:22

3 Answers 3

3

When you run names[i]=key; you don't really copy key's string value to names[i].
It just makes name[i] point to where key is (since both name[i] & key are pointers).

so all in all you're overwriting key several times, and making all of names pointers point to key.

You need to copy those strings either by working with std::string instead of char* or by using strcpy. I'd recommend on working with std::string.


Using std::string your code should look like this:

#include <iostream>
#include <string>
using namespace std;

const int MAX = 4;

int main ()
{
  string names[4];

  for (int i = 0; i < MAX; i++)
  {
    cout << "entr keys" << endl;
    cin >> names[i];
    cout << names[i];
  }
  for(int i=0; i<4;i++)
  {
    cout << names[i];
  }
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Every time you execute the lines

cout << " entr keys\n";
cin >> key;

you're inserting a null-terminated string into key, e.g. "hello\0".

Afterwards you copy key's address and store it into a cell of the names pointers array:

names[i]=key; // Now I point to 'key'
cout<< names[i];

then the cycle starts again. Anyway from the second time on you're inserting null-terminated strings into key and thus overwriting the previous contents. The second time if you had entered "hi\0" the contents of the key array would become

['h', 'i', '\0', 'l', 'l', 'o', '\0']

anyway you're going to only print the first string since the null terminator will prevent the other content from being displayed.

When the program ends you're going to have four pointers to the same key array and that array will only contain the last element inserted which overwrote the previous ones.

In order to solve you can make your array a bidimensional one (or use a string array):

const int MAX = 4;

int main ()
{
  char key[4][20]; // <- Now this has two indices
  char *names[4];

  for (int i = 0; i < MAX; i++)
  {
    cout << " entr keys\n";
    cin >> key[i];
    names[i]=key[i];
    cout<< names[i];
  }
  for(int i=0; i<4;i++)
  {  
    cout << names[i];
  }
  return 0;
}

Live Example

Comments

1

Corrected program:

#include <iostream>
using namespace std;
#include <cstring>

const int MAX = 4;

int main ()
{
  char key[20];
  char *names[MAX];

  for (int i = 0; i < MAX; i++)
  {
    cout << " entr keys\n";
    cin >> key;
    names[i] = new char[strlen(key) + 1];//names[i]=key;
    strcpy(names[i], key);
    cout<< names[i];
  }
  for(int i=0; i<MAX;i++)
  {  
    cout << names[i];
  }
    for(int i=0; i<MAX;i++)
  {  
    delete [] names[i];
  }
  return 0;
}

You need to allocate space for each names[i] and when done, deallocate also, changed the hardcoded 4 to MAX

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.