Changing string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yaru22

    #1

    Changing string

    Hi, I'm relatively new to C programming and was quite confused by this
    error.

    What I tried to do is that if I have a string with double quotes
    around it (i.e. char *str = "\"hello\"" ;), I have to remove the
    quotes.

    So I programmed this as follow:

    #include <stdio.h>
    #include <string.h>

    int change(char *str) {
    int len = strlen(str);
    if (str[0]=='\"' && str[len-1]=='\"') {
    char str_tmp[len+1];
    strncpy(str_tmp ,str+1,len-2);
    str_tmp[len-2]='\0';
    *str = *str_tmp;
    }
    return 0;
    }

    int main() {
    char *str = "\"hello\"" ;
    change(str);
    printf("%s\n",s tr);
    return 0;
    }

    However, if I run this code, I get segmentation fault error.

    I guess "*str = *str_tmp;" part is wrong, but I don't know how to fix
    it.

    Doesn't this statement mean, substitute the value of str_tmp into the
    memory location that str is pointing to?

    I wonder what the reason for the error and how to do it.

    Also, the return type of the function "change" should be int.

    Thanks in advance.

    Brian

  • Eric Sosman

    #2
    Re: Changing string

    yaru22 wrote On 03/06/07 10:27,:
    Hi, I'm relatively new to C programming and was quite confused by this
    error.
    >
    What I tried to do is that if I have a string with double quotes
    around it (i.e. char *str = "\"hello\"" ;), I have to remove the
    quotes.
    >
    So I programmed this as follow:
    >
    #include <stdio.h>
    #include <string.h>
    >
    int change(char *str) {
    int len = strlen(str);
    if (str[0]=='\"' && str[len-1]=='\"') {
    char str_tmp[len+1];
    strncpy(str_tmp ,str+1,len-2);
    str_tmp[len-2]='\0';
    *str = *str_tmp;
    }
    return 0;
    }
    >
    int main() {
    char *str = "\"hello\"" ;
    change(str);
    printf("%s\n",s tr);
    return 0;
    }
    >
    However, if I run this code, I get segmentation fault error.
    This is Question 1.32 in the comp.lang.c Frequently
    Asked Questions (FAQ) list at <http://www.c-faq.com/>.


    --
    Eric.Sosman@sun .com

    Comment

    • Arun

      #3
      Re: Changing string

      On Mar 6, 10:31 am, Eric Sosman <Eric.Sos...@su n.comwrote:
      yaru22 wrote On 03/06/07 10:27,:
      >
      >
      >
      >
      >
      Hi, I'm relatively new to C programming and was quite confused by this
      error.
      >
      What I tried to do is that if I have a string with double quotes
      around it (i.e. char *str = "\"hello\"" ;), I have to remove the
      quotes.
      >
      So I programmed this as follow:
      >
      #include <stdio.h>
      #include <string.h>
      >
      int change(char *str) {
      int len = strlen(str);
      if (str[0]=='\"' && str[len-1]=='\"') {
      char str_tmp[len+1];
      strncpy(str_tmp ,str+1,len-2);
      str_tmp[len-2]='\0';
      *str = *str_tmp;
      =The above statement will assign the first char in str_tmp to first
      char of str.
      =Use strcpy(str,str_ tmp) instead. This will fix the logical error.
      }
      return 0;
      }
      >
      int main() {
      char *str = "\"hello\"" ;
      =Specify to your compiler that this is a writable string by
      =char str[] = "\"hello\"" ;
      change(str);
      printf("%s\n",s tr);
      return 0;
      }
      >
      However, if I run this code, I get segmentation fault error.
      >
      This is Question 1.32 in the comp.lang.c Frequently
      Asked Questions (FAQ) list at <http://www.c-faq.com/>.
      >
      --
      Eric.Sos...@sun .com- Hide quoted text -
      >
      - Show quoted text -
      -Arun Joseph

      Comment

      • ComerHides@gmail.com

        #4
        Re: Changing string

        Replace the code

        *str = *str_tmp; by

        str = str_tmp;

        and check the output


        Comment

        • Flash Gordon

          #5
          Re: Changing string

          ComerHides@gmai l.com wrote, On 06/03/07 16:28:
          Replace the code
          >
          *str = *str_tmp; by
          >
          str = str_tmp;
          >
          and check the output

          Firstly, please quote sufficient of what you are replying to so that
          people know what you are refering to. There is no guarantee that people
          have, or ever will, see the message you are replying to.

          Secondly, try your suggestion and you will see that it does not work.
          Then search through the comp.lang.c FAQ for the question and answer that
          tells you why it does not work.
          --
          Flash Gordon

          Comment

          Working...