Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning values to a string pointer

Tags:

c++

string

I am currently analysing the char and string datatypes. For char data types, the following code snipped holds good:

char value = 'a';
char value1[] = "Good";
char* value2 = "Good";

For strings,

string strValue = "Good";
string strVal[3] = {"Good","Better","Best"};

But the assignment below throws the compilation error:

"error: scalar object strPtr requires one element in initializer"

string* strPtr = {"Good","Better","Best"}

So, how to assign values to above string* initially?

Thanks, Udhai

like image 309
udhai_coder Avatar asked Apr 28 '26 23:04

udhai_coder


1 Answers

Pointers are not arrays, so why would you want to pretend otherwise? You could do this however

string strVal[3] = {"Good","Better","Best"};
string* strPtr = strVal;
like image 134
jahhaj Avatar answered Apr 30 '26 14:04

jahhaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!