Changing string

Discussion in 'C Programming' started by yaru22, Mar 6, 2007.

  1. yaru22

    yaru22 Guest

    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",str);
    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
     
    yaru22, Mar 6, 2007
    #1
    1. Advertising

  2. yaru22

    Eric Sosman Guest

    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",str);
    > 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, Mar 6, 2007
    #2
    1. Advertising

  3. yaru22

    Arun Guest

    On Mar 6, 10:31 am, Eric Sosman <> wrote:
    > 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",str);
    > > 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/>.
    >
    > --
    > - Hide quoted text -
    >
    > - Show quoted text -


    -Arun Joseph
     
    Arun, Mar 6, 2007
    #3
  4. yaru22

    Guest

    Replace the code

    *str = *str_tmp; by

    str = str_tmp;

    and check the output
     
    , Mar 6, 2007
    #4
  5. yaru22

    Flash Gordon Guest

    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
     
    Flash Gordon, Mar 6, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    525
    Alvin Bruney
    Nov 30, 2003
  2. Mladen Adamovic
    Replies:
    0
    Views:
    739
    Mladen Adamovic
    Dec 4, 2003
  3. Pokerkook
    Replies:
    4
    Views:
    357
    Lawrence Kirby
    Dec 10, 2004
  4. Aaron Beall
    Replies:
    2
    Views:
    1,818
    Aaron Beall
    Sep 14, 2007
  5. VK
    Replies:
    3
    Views:
    179
    Dr J R Stockton
    Mar 15, 2007
Loading...

Share This Page