Array + cin

Is there a way to store the string entered through cin and store it into an array of char that is exactly the same size? Basically creating an array like: "char a[];" and using it to store cin. If so, how?

1
2
3
string a = "apples";
char b[6];
b=a;

Is the above valid? I know that when a string is converted to a char array, they add a \0 to the end of the array, but since the array size is exactly the same as the char, what happens?
closed account (zvRX92yv)
All strings, no matter the container, (usually :p) is terminated with 0x00. (\0)
Because, say, you want to keep outputting something, char by char.
How will you know when to end? You don't, unless you have a null terminator, 0x00.

Trying to do that will give you an out of bounds error.
And, you can't even do that, you will have to do - b = a.c_str(); to convert it to a char array, since std::string is a class.

And, to answer your question, yes, you can. I'm not sure but I think you can use a pointer, after all, array (headers?) are just pointers to a point in the memory where it starts.

array (headers?) are just pointers to a point in the memory where it starts.


No, not quite. It's an address, but not a pointer. You can pass the address of the array to the pointer, but not the pointer to the array:

char array[10];
char *pointer;

pointer = array;
array = pointer;

In the first, it assigns the address of array to pointer and perfectly legal.
In the second part, there is an attempt to change the address of a static memory block. No can do.

Pointers have more flexibility and it's easier to hang yourself with pointers, but I wouldn't want to be without them.

As for the original post, that can't be done as a string is not really even a char array. It's a class and a char is a fundamental base type.

As for assigning a char array to another char array, no can do. They are both statically defined memory blocks and immobile.
Last edited on
Still not sure what you guys said. Anyone can simplify it?
You want to convert a std::string to an char array, right?

Not sure why you want that but if you actually have a good reason (I mean search a bit if you actually want this or you just think you do) the 2 member functions are these:
http://www.cplusplus.com/reference/string/string/data/
http://www.cplusplus.com/reference/string/string/c_str/

data() and c_str() do exactly the same except the final \0 being added or not. The c_str() adds the \0, data() is not.

I insist what exactly do you want a c string that std::string cannot do?
Last edited on
You can use a vector of strings. So your idea shown in your code

1
2
3
string a = "apples";
char b[6];
b=a;


can be realized with using std::vector. For example


1
2
3
4
5
6
7
8
9
10
#include <string>
#include <vector>

int main()
{
   std::vector<std::string> v;
   std::string s( "apples" );

   v.push_back( s );
}



As for a character array you can use the following code template

1
2
3
4
5
std:;string s( "apples" );

char *p = new char[s.size() + 1];

std::strcpy( p, s.c_str() );
Last edited on
Topic archived. No new replies allowed.