CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Standard C strings

    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:

    Code:
    wchar_t *strmani(wchar_t *input){
       for(int i=0;i<(int)wcslen(input);i++){
            input[i] = dosomething;
      }
    
      return(input);
    }
    This works well if you call it like this:

    Code:
    wchar_t mybuffer[64];
    wcscpy(mybuffer, L"manipulate me\0");
    strmani(mybuffer);
    It does of course not work if you call strmani() like:
    Code:
    strmani(L"my string without buffer\0");
    It will crash.
    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:

    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);
    }
    For my surprise, this actually works even if you return an address of a local defined variable.
    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
    Last edited by CatShoe; December 22nd, 2008 at 09:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured