Smartypants
Smartypants

Reputation: 55

Issue with a char* array

Okay so I have:

char* arr[5];

and I have

char input[10];

and then:

int i = 0;
cin.getline(input, 10);
while(input[0] != 'z')
{
    arr[i] = input;
    cin.getline(input, 10);
    i++;
}

the result is that every element in arr is the same because they are all pointers to input, but I want each element to point to char arrays that the input variable held at that given time.

result that this brings:

arr[0] = line beginning in 'z' (because that is what input currently is holding)
arr[1] = line beginning in 'z'
arr[2] = ... and so on

result that I want:

arr[0] = first line read in
arr[1] = second line read in
arr[2] = third line read in and so on...

I am confused about how I can get the elements to all point to new values instead of all be pointing to the same value.

Upvotes: 0

Views: 85

Answers (3)

Ashish Surana
Ashish Surana

Reputation: 95

If you want to take input 5 times,you can try this code segment

int i = 0;
while(i<5)
{
    arr[i] = input;
    cin.getline(input, 10);
    i++;
}

this should work and you can get your "desired result" as you stated above.

Edit: This will not work, as described in the comment. See example here: http://ideone.com/hUQGa7

What is required are different pointer values occupying each of the elements in arr. How to achieve those different pointer values is discussed in the other answers given.

Upvotes: 2

Thomas Matthews
Thomas Matthews

Reputation: 57749

Let's talk characters and pointers.

Given a character:

+---+  
+ A +  
+---+  

A pointer to the character, char *, points to the character. That's it, nothing more.

An array of characters is a container that has slots for characters. A pointer to the array often points to the first character of the array. Here's where the problem comes in.

Many functions require a pointer, to the first character of the array, but either don't say that it's to the first character or require a pointer to a single character, assuming that the pointer is the first character in the array.

Pointers need something to Point at

You have allocated an array of pointers, but haven't allocated the memory for each pointer:

Array
+-----+      +----+---+---+  
|     | -->  |    |   |   |  
|     |      +----+---+---+  
+-----+ 
|     |      +----+---+---+
|     | -->  |    |   |   |  
|     |      +----+---+---+  
+-----+ 

The content of the array are pointer. So you will need to allocate memory and place the pointer into the array:

  arr[0] = new char [11]; // +1 for the nul terminator character.
  char text[33];
  arr[1] = text;  

So what you aim to do is:

  cin.getline(arr[0], 10);
  cin.getline(arr[1], 33);

Strings are soooo much easier to deal with. They manage their own memory:

  std::string array_text[5]; // An array of 5 string.
  getline(cin, array_text[0]);  

The next step is to use a vector and you are all caught up:

  std::vector< std::string > text_vector(5);  // Pre-allocate 5 slots.
  getline(cin.text_vector[2]);

Upvotes: 0

Arpit
Arpit

Reputation: 775

You have to copy the data of input everytime.

You can do like this in while loop:

while(input[0] != 'z')
{
    arr[i] = new char[strlen(input)];
    strcpy(arr[i], input);

    cin.getline(input, 10);
    i++;
}

And since you have defined arr to be of length 5. So you have to check in the while loop that i doesn't exceed the value 5. i<5.

Upvotes: 0

Related Questions