I want to declare an array of pointers with a character without using a string

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 keys and print them in the first loop for

, they show the correct value, but when I type names[i]

in the second loop for

, it keeps showing the last key entered over and over.

Please tell me: where am I going wrong?

+3


source to share


3 answers


When you run names[i]=key;

, you don't really copy the string value key

to names[i]

.
He simply points name[i]

out where the key (as both are name[i]

and key

are pointers).

so in all cases you are rewriting key

multiple times and all pointers names

point to the key.

You need to copy these lines either with std::string

instead char*

or with strcpy

. I would recommend 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;
}

      

+3


source


Every time you execute lines

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

      

you are inserting a null character string in key

eg. "hello\0"

...

Then you copy the address key

and store it in a location in the name pointers array:

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

      

then the cycle starts again. Anyway, from the second time you insert null-terminated strings into the key and thereby overwrite the previous content. Second time, if you entered "hi\0"

, the contents of the array key

will become



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

      

you are only going to print the first line anyway, as the null delimiter will prevent other content from being displayed.

When the program ends, you will have four pointers to the same key array, and this array will only contain the last inserted element that overwrites the previous ones.

To solve the problem, you can make your array two-dimensional (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

+3


source


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 name [i], and when done, make space too, changed hardcoded 4 to MAX

+1


source







All Articles