Thread: sscanf replacing chars

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    sscanf replacing chars

    does anyone know how i can use sscanf to replace characters in a string?


    say i have string

    abcd

    and i want to replace a and d with a space, how coul di do that?


    thanks




    also does anyone know why im getting this warning?
    Code:
    char * func1() { 
    
    char blah[30];
    
    return blah;
    
    }
    warning C4172: returning address of local variable or temporary
    Last edited by paperbox005; 05-03-2005 at 12:39 AM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    about your second warning message, change the code as below

    Code:
    char * func1() { 
    
    static char blah[30];
    
    return blah;
    
    }

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    awe3some thanks!

    im not too sure how that works, but it does.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    basically, you were returning the address of the variable which goes out of scope after the function returns. The memory pointer that your are returning to calling function is in "dangling" state after that. If its good till the time you use it in calling function, you are lucky. Otherwise , if its currpted or allocated to some other process, your calling function accessing it might produce crash.

    Static solves the problem by making its scope persistant even when the function declaring it goes out of scope. These variables are stored in a seperate locations in memory and are valid throughout the program scope.
    Last edited by nkhambal; 05-03-2005 at 01:35 AM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by paperbox005
    does anyone know how i can use sscanf to replace characters in a string?
    First, I wouldn't use sscanf.
    Quote Originally Posted by paperbox005
    say i have string

    abcd

    and i want to replace a and d with a space, how coul di do that?
    I'd just loop through the source string looking for the character to find and replace it with the desired string.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *dst, const char *src, char find, const char *replace)
    {
       char *start = dst;
       while ( *src )
       {
          if ( *src == find )
          {
             strcpy(dst, replace);
             dst += strlen(replace);
          }
          else
          {
             *dst++ = *src;
          }
          ++src;
       }
       *dst = '\0';
       return start;
    }
    
    int main()
    {
       static const char before[] = "abcdabcdabcd";
       char after [ sizeof before * 2 ];
       puts(foo(after, before, 'a', "d "));
       return 0;
    }
    
    /* my output
    d bcdd bcdd bcd
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  2. Replacing and finding chars in a string
    By xshapirox in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2004, 11:40 PM
  3. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  4. Replacing chars in Pointer String results in Segfault
    By discostu in forum C Programming
    Replies: 2
    Last Post: 03-08-2003, 03:38 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM