basic string question
-
Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance
strncpy(Arr, lpcstr, 255); Arr[254] = 0; // just to be safe Cleek | Image Toolkits | Thumbnail maker
-
Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance
Returning a char array this way is tricky. Either you have to allocate the memory inside the function and free it outside (which is very ugly), or you can pass the pointer as a parameter of the function and manipulate its content. Anyway, it is much better to use the std::string (from the Standard Template Library) for which you don't need to care about memory usage.
Cédric Moonen Software developer
Charting control -
Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance
If you can modify the function (that is, you control it), you can make the return value part of the function arguments:
void getMyString(char[255]& retString) { memset(retString, 0, 255); // do stuff here // declare and initialize srcString somewhere strncpy(retString, srcString, 254); }
If you don't have access, or are not allowed to change the interface, here are some alternatives:
char buffer[255] = {0}; strncpy(buffer, myFunction(), 254); std::string myString = myFunction();
Note that both of those can be sketchy if the returning function is allocating memory on the heap and expecting you to handle cleanup (very poor design, but not too uncommon, sadly). Just keep that in mind when dealing with this kind of thing. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
If you can modify the function (that is, you control it), you can make the return value part of the function arguments:
void getMyString(char[255]& retString) { memset(retString, 0, 255); // do stuff here // declare and initialize srcString somewhere strncpy(retString, srcString, 254); }
If you don't have access, or are not allowed to change the interface, here are some alternatives:
char buffer[255] = {0}; strncpy(buffer, myFunction(), 254); std::string myString = myFunction();
Note that both of those can be sketchy if the returning function is allocating memory on the heap and expecting you to handle cleanup (very poor design, but not too uncommon, sadly). Just keep that in mind when dealing with this kind of thing. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Zac Howland wrote:
void getMyString(char[255]& retString)
That won't work.
-
Zac Howland wrote:
void getMyString(char[255]& retString)
That won't work.
You are correct. Sorry, it should have been:
void getMyString(char retString[255])
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac