Question with c++,string and arrays?
I want your opinions and also a solution for my underlying problem if possible.
Here is the problem. I have a structure say Quote:
strcpy(user_property[0].name,"value1"); strcpy(user_property[0].value,((const char*)Getvalue1())); Here Getvalue1() is a memeber function of a class that returns a string value I have to actually run a loop to assign 15 such values.This can be done if I pass array indexes so first I have to store the values value1 etc and (const char*)Getvalue1() in some array but I am not sure how to do this I tried doing the same but I have the following compilation errors This is the sample code that I have done PHP Code:
PHP Code:
at line PHP Code:
Regards, Ajit |
Use "sample1.c_str()" instead of just "sample1". c_str() returns a pointer to a char buffer representing the underlying string - it's guaranteed not to change so long as you only use const methods on the string (if I remember correctly).
Also, consider abandonning strcpy() and using strncpy(), it's safer (i.e. will save you headaches later on). |
@ the OP,
Unless you are developing real-time code, I would also recommend abandoning character arrays for storing strings. Just store them within an STL string. Code:
struct Property |
Thanks
Hi dwhitney67 and dwhitney67 thanks for your responses.That helps.
Ajit |
All times are GMT -5. The time now is 04:02 PM. |