Truth be told, i wasn't aware they was classed as two different things (array of chars/cstrings), i assumed they were just different ways of declaring an array of chars.
Well, I think I made up that distinction, but unless you understand it, I don't think you will ever understand char arrays. You are right, they are just two different ways of declaring char arrays, but a cstring has a '\0' at the end of the array, and an array-of-chars doesn't. In fact, you can actually create an array-of-chars that is a cstring:
char str[] = {'a', 'b', 'c', '\0'};
and to test it out:
cout<<str<<endl;
You could also use all the <cstring> functions on str.
The use of arrays wasn't a problem as my previous experience with PHP covered the theory side of things
Every computer language has arrays, but with C++ you will learn about the 'real' theory behind them. In PHP, you can't really know how arrays are stored in memory because PHP doesn't have pointers. PHP is actually just a computer program written in C++ that provides you with an easier syntax.
Here is a crucial piece of information I left out of my previous description: a char array name is actually a pointer. That's why at the beginning I said, you have to understand pointers to truly understand char arrays. A pointer variable is just a variable with a strange looking syntax that stores the address of some data in memory. Remember when I said that C++ automatically slaps a '\0' character onto the end of a string literal in this statement:
char text[] = "some text";
Well, I left out a step. C++ slaps a '\0' character onto the end of the string literal, and then it stores:
some text\0
in memory somewhere. After that, C++ assigns the address of that spot in memory to the variable name: text. However, it is very hard to know that. With a normal pointer variable, when you display it using cout<< you will get the address. For instance,
Code:
int num = 10;
int* p; //declare a pointer variable p(with that strange looking syntax)
p can now store the address in memory of any int variable, like the variable num:
p = # //the & operator gets the address of num, which is then assigned to p
Now if you use cout<< to display p:
cout<<p;
you will get some strange looking output, something like: 006BFDF4. That is the address in memory of the value 10. Normally, when you output a variable using cout<<, you expect to get whatever is stored in the variable displayed to the console. However, with a pointer to type char, the << operator in the instruction cout<< is defined NOT to output what is stored in the variable(which is the address)--instead it gets the address from the variable and goes to that location in memory and fetches the value stored there and displays that to the console.
cout<<text; //some text
For other pointer types, to get the value stored in memory at the address contained in a pointer variable, you have to do what's called "dereferencing the pointer":
Code:
int num = 10;
int* p; //declare a pointer variable p(with that strange looking syntax)
p = # //the & operator gets the address of num, which is then assigned to p
cout<<*p; //"dereference the pointer" which gets the address stored in p, goes
//to that location in memory, and fetches the value there.
That is why it is hard to tell that text is a pointer--the <<operator in the instruction cout<< automatically dererefences pointers to type char, and you can't stop cout<< from doing that. Since you can't display the address stored in the char array variable name, the name doesn't appear to be a pointer. However, you can see a glimmer that a char array name is a pointer when you try to do something like this:
Code:
char text[] = "some text"; //cstring
if(text == "some text")
{
cout<<"they are equal\n";
}
else
{
cout<<"NOT equal\n";
}
The reason the variable text and the string literal "some text" aren't equal is because text is a pointer that stores an address that looks like: 006BFDF4, so the if statement is really this:
if(006BFDF4 == "some text")
Just because the <<operator in the instruction cout<< automatically converts text, which is an address, to the value stored at the address doesn't mean all operators do that, and in fact the == operator does not make that conversion.
Confusing, eh?