I was just wondering why it would be necessary to create an array of pointer-to-chars whose elements are assigned to strings. Not only that but a const pointer-to char...I read that in this C++ book I'm using to teach myself and I was just curious why that was necessary....any answers/insight would be great. thanks-Chap
const char* qualify =
The type of a string literal is array of const char, so to be strictly correct, you must use a pointer to const char to point to a string literal.
>I was just wondering why it would be necessary to create an array of pointer-to-chars whose elements are assigned to strings.
It isn't necessary. There are a number of ways to go about having an array of strings, this is just one of them. But the idea is that if the strings don't need to be modified, you can simply assign them to pointers and avoid having to specify an array size.
Each character array (c-style string) needs a pointer. (A single variable can only hold one character, so you use a single pointer to point-to the array.)
4 character arrays require 4 pointers. In this case, an array of 4 pointers is used. The program could have used 4 different variables instead of an array (i.e. qualify1 instead of qualify).
The pointers are constant, so you don't screw them up. You don't want to accidently change one of the pointers. Because of the way arrays are stored in memory, changing the pointers could really foul things up!
"String1 \n" is a string literal. It can't be changed. It's value can be assigned to other strings, and if you don't want the other string to change the value either then you need to use the keyword const in front of the keyword char.
const char * means you have a pointer to a char and the value of the char can't change.
char * can be a pointer to a single char, or a pointer to the first element of a string of char. You can't tell until it is initialized which it is.
char * one = "string1 \n"; //one is a string;
char ch = '+';
char * one = &ch; //one is a pointer to a single char
const char * one = "string1"; //one is a string and you can't change the value of the string
const char one = "string1"; //one is a string and you can't change the value of the string
You can have arrays of arrays, arrays of pointers, pointer to arrays, const variables, const pointers, pointers to const variables, const pointers to const variable, pointers can act like arrays, etc., etc., etc. It's kind of confusing to begin with. What am I saying! It's very confusing to begin with and remains kind of confusing forever--unless you do this kind of thing all the time. But you get used to going back to the reference material time after time, until you get the hang of it.
In this case the author has declared a array of 4 C style strings, and the value of the strings cannot be changed. He/she could have allowed the values to be changed if they wanted to. They could have used alternate synatx, maybe something like this:
(if that's even legal), but chose to use the one they did. I can't say why they chose that syntax over another. It's easy to see why using a vector of STL strings is a commonly used alternative to this type of syntax. Squirming your way through the nuts and bolts at this level can prepare you for understanding what's behind the curtain when you start to use the vectors and STL strings, though. So if you like that approach, continue. If not you can learn about vectors and STL strings first and then look behind the curtain later if you want to.