Hello Folks,
I have some trouble figuring this out correctly. If you have a function that should
manipulate a string in some way, you probably do it like this:
This works well if you call it like this:Code:wchar_t *strmani(wchar_t *input){
for(int i=0;i<(int)wcslen(input);i++){
input[i] = dosomething;
}
return(input);
}
It does of course not work if you call strmani() like:Code:wchar_t mybuffer[64];
wcscpy(mybuffer, L"manipulate me\0");
strmani(mybuffer);
It will crash.Code:strmani(L"my string without buffer\0");
Now I was wondering if you can write a function that handles both, buffers and "plain"
strings without a buffer defination.
So I tried this:
For my surprise, this actually works even if you return an address of a local defined variable.Code:wchar_t *strmani(wchar_t *str){
wchar_t buf[255];
wcscpy_s(buf, 255, str);
for(int i=0;i<(int)wcslen(buf);i++){
buf[i] = dosomething;
}
return(buf);
}
But I'm not happy with that, some compilers (espcially gnu's) gave me at least a compiler
warning for exactly that reason.
So now after all this my question, is this a valid possibility if I don't want to care whether
someone calls strmani(var) or strmani(L"text")?
Is there maybe a better solution?
Thanks in Advance,
Andy

