I am trying to learn how to pass char, char*, char*[] into functions and such operations but.....I just don't understand!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Parent{
private:
char* text[];
// I want to have an array of char pointers so I can output
// EX. text[0]="Hello", text[1]="World" etc
public:
Parent( char* t[] ){
//Constructor takes char* [] type to I can initialize 'text'
}
char* getText()[ return text; ] //I am not sure how to return 'text'
}
int main(){
char * input[] = {"Hello", "World"};
Parent object = new Parent(input);
}
The error message I am getting is giving me a headache about "char*" not "char**" or can't assign "char*" to "char[0]"
If you absolutely must use C-style strings and arrays, then text is an array of char*. You can't return it as a single char*, any more than you could return an arrtay of int as a single int.
An array decays to a pointer of the type of element it contains. An array of int would decay to an int*. Similarly, an array of char* decays to a pointer to char*, i.e. char** .
But, as JLBorges says, in C++ we would prefer to use std::string instead of C-style strings (and also, std::vector instead of C-style arrays).
class Parent
{
private:
char** text;
// I want to have an array of char pointers so I can output
// EX. text[0]="Hello", text[1]="World" etc
public:
Parent( char** t )
{
text = t;
}
char** getText(){ return text; } //I am not sure how to return 'text'
};
int main()
{
char * input[] = {"Hello", "World"}; //you are cheating a bit, nonstandard compiler extension
// you can't convert literals to char* in standard c++.
//Parent object = new Parent(input); //this looks like another similar language ;)
Parent object(input);
cout << object.getText()[0] << " " << object.getText()[1] << endl;
}
^^ this feels like taking the area of a rectangle with calculus.
the big take-aways..
1) char * bad. string good.
2) the type of get text and the type of text need to match, and they need to be something the compiler can work with at compile time. [] empty/flexible sized array syntax is tricky to get working outside of trivial examples, because there are rules about how arrays can be passed around. Pointers can avoid these rules, but you have to be very careful about how you do this. The better solution is to pass containers (and string is a container) around which avoid these aggravations. I like C-arrays, but one of their biggest frustrations / weaknesses is passing them with more than one dimension: it is harder than it should be to do that, and you need some background to do it. Its better if you just avoid that issue and pass containers instead. You can make these things work, of course, just be aware that you have lapsed into C an will have to be a lot more hands-on to pull it off.