0

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.

8
  • 2
    Stop now with char * and use std::string. You'll be glad you did. Commented Sep 14, 2014 at 17:45
  • I can't. It is a stipulation in the project I am working on. Commented Sep 14, 2014 at 17:47
  • @Smartypants - It is a stipulation in the project I am working on. So you're learning C instead of C++. Great. Commented Sep 14, 2014 at 18:03
  • just for this first project xD Commented Sep 14, 2014 at 18:16
  • @Smartypants - So what else can't you use? Maybe you should state up front what parts of the C++ language and library are off-limits (as ridiculous as that sounds). Commented Sep 14, 2014 at 18:27

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This will not work. You're storing a pointer that never changes, so at the end, you wind up with the last data entered for all entries in arr. See here: ideone.com/hUQGa7
0

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]);

Comments

0

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.

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.