Board index » cppbuilder » string vs. char *
Edmund
CBuilder Developer |
string vs. char *2007-11-13 03:25:53 PM cppbuilder80 Hi, Pardon my ignorance as I'm a Delphi programmer trying a go at C++. I've had a bit of a C background (when I meant 'a bit', I meant that I can create a 'Hello World' program. *grin*) and when I studied C a while back, I had *major* problems with pointers. I don't know if my brain can't cope with pointers or that I haven't paid too much attention to the usage. Anyway, if I have a function that takes a string as a parameter and returns a 'modified version' of this string, would the following be ok? char * myfunc( char * sParam); With the issue of 'buffer overflows' and possible bad programming practices in mind, I can't figure out how I would return a modified string. I mean, if I do declare a local variable within the function, the scope stays within the function. Returning it to the place that calls the function might prove to be hazardous? i.e. char * myfunc ( char * sParam){ char * tempstr; tempstr = strcpy(sParam,0,strlen(sParam)-1); return (tempstr); } As far as I know, I'm returning a locally-defined address back to the 'global' scope (depending if the calling line is in the main() or within another function). By the time the function quits, wouldn't that address have been 'forgotten'? i.e. Any future accesses/use of the returned address will produce a memory access AV? Now, as far as I know, that is the *hard* way of doing strings. The easy way, as far as I've gathered, is to use the <string>headers. Since I've been 'spoiled' by Delphi in terms of string usage, the <string>header would certainly be a heaven-sent library. However, in terms of memory usage/efficiency, what method of 'doing strings' would be better? char *? or <string>? Any clarifications appreciated. Ed } |