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?
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.