A strange string function error

Discussion in 'C Programming' started by jacklisp@gmail.com, Nov 7, 2008.

  1. Guest

    I have wrote a small function but got the error
    function:
    /*< squeeze */
    void squeeze(char s1[],char s2[])
    {
    int de_s ;
    int i,j,k;

    for(i = 0;s2 !='\0';++i)
    {
    de_s = s2;

    for(j = k = 0;s1[j] != '\0';++j)
    {
    if(s1[j] != de_s)
    {
    s1[k++] = s1[j];
    }


    }
    s1[j] = '\0';


    }
    //printf("s1:%c",s1[0]);


    }
    /*>*///end-squeeze
    the function just deletes each character in s1 that matches any
    character in the string s2.(exercise from the c language
    programming).But it doesn't work and I got
    the message form GDB:
    Starting program: /home/jack/myPro/myC/ListTest/CprogrammingTest/
    Char01/main

    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400659 in squeeze (s1=0x40098b "abcdefb", s2=0x400988
    "ab") at main.c:40

    I use squeeze("abcdefb","ab") to call the function.
    Any suggestion?
    Thanks :)
     
    , Nov 7, 2008
    #1
    1. Advertising

  2. Poter Guest

    On Nov 7, 6:55 pm, "" <> wrote:
    > I have wrote a small function but got the error
    > function:
    > /*< squeeze */
    > void squeeze(char s1[],char s2[])
    > {
    >         int de_s ;
    >         int i,j,k;
    >
    >         for(i = 0;s2 !='\0';++i)
    >         {
    >                 de_s = s2;
    >
    >                 for(j = k = 0;s1[j] != '\0';++j)
    >                 {
    >                         if(s1[j] != de_s)
    >                         {
    >                                s1[k++] = s1[j];
    >                         }
    >
    >                 }
    >                 s1[j] = '\0';
    >
    >         }
    >         //printf("s1:%c",s1[0]);
    >
    > }
    >
    > /*>*///end-squeeze
    > the function just deletes each character in s1 that matches any
    > character in the string s2.(exercise from the c language
    > programming).But it doesn't work and I got
    > the message form GDB:
    > Starting program: /home/jack/myPro/myC/ListTest/CprogrammingTest/
    > Char01/main
    >
    > Program received signal SIGSEGV, Segmentation fault.
    > 0x0000000000400659 in squeeze (s1=0x40098b "abcdefb", s2=0x400988
    > "ab") at main.c:40
    >
    > I use squeeze("abcdefb","ab") to call the function.
    > Any suggestion?
    > Thanks :)


    why "int de_s ;" but not "char des_s;"?
     
    Poter, Nov 7, 2008
    #2
    1. Advertising

  3. Chris Dollin Guest

    wrote:

    > I have wrote a small function but got the error
    > function:
    > /*< squeeze */
    > void squeeze(char s1[],char s2[])
    > {
    > int de_s ;
    > int i,j,k;
    >
    > for(i = 0;s2 !='\0';++i)
    > {
    > de_s = s2;


    (Why not declare `de_s` here, rather than in the outer loop?)

    (Why is it `int`, not `char`?)

    (Why is it called `de_s`?)

    >
    > for(j = k = 0;s1[j] != '\0';++j)
    > {
    > if(s1[j] != de_s)
    > {
    > s1[k++] = s1[j];


    You're writing into the string `s1`.

    > }
    >
    >
    > }
    > s1[j] = '\0';
    >
    >
    > }
    > //printf("s1:%c",s1[0]);
    >
    >
    > }
    > /*>*///end-squeeze
    >
    > I use squeeze("abcdefb","ab") to call the function.


    You pass the string literal `"abcdefb"` as the value for the
    argument `s1`.

    You are not allowed to write into string literals.

    You were lucky: BOOM.

    > Any suggestion?


    Don't write into string literals, and don't use so many blank lines
    and whitespace in your code.

    void squeeze(char s1[],char s2[])
    {
    int i,j,k;
    for(i = 0; s2 !='\0'; ++i)
    {
    char de_s = s2;
    for(j = k = 0; s1[j] != '\0'; ++j)
    {
    if(s1[j] != de_s) s1[k++] = s1[j];
    }
    s1[j] = '\0';
    }
    //printf("s1:%c",s1[0]);
    }

    I won't comment on the algorithm itself.

    --
    "Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
     
    Chris Dollin, Nov 7, 2008
    #3
  4. Ian Collins Guest

    wrote:
    > I have wrote a small function but got the error
    > function:
    > /*< squeeze */
    > void squeeze(char s1[],char s2[])
    > {

    <snip>
    > }
    > /*>*///end-squeeze
    > the function just deletes each character in s1 that matches any
    > character in the string s2.(exercise from the c language
    > programming).But it doesn't work and I got
    >
    > Program received signal SIGSEGV, Segmentation fault.
    > 0x0000000000400659 in squeeze (s1=0x40098b "abcdefb", s2=0x400988
    > "ab") at main.c:40
    >
    > I use squeeze("abcdefb","ab") to call the function.
    > Any suggestion?


    Don't pass string literals to functions that expect modifiable arguments.

    --
    Ian Collins
     
    Ian Collins, Nov 7, 2008
    #4
  5. Guest

    On Nov 7, 7:21 pm, Chris Dollin <> wrote:
    > wrote:
    > > I have wrote a small function but got the error
    > > function:
    > > /*< squeeze */
    > > void squeeze(char s1[],char s2[])
    > > {
    > > int de_s ;
    > > int i,j,k;

    >
    > > for(i = 0;s2 !='\0';++i)
    > > {
    > > de_s = s2;

    >
    > (Why not declare `de_s` here, rather than in the outer loop?)
    >
    > (Why is it `int`, not `char`?)
    >
    > (Why is it called `de_s`?)
    >
    >
    >
    > > for(j = k = 0;s1[j] != '\0';++j)
    > > {
    > > if(s1[j] != de_s)
    > > {
    > > s1[k++] = s1[j];

    >
    > You're writing into the string `s1`.
    >
    > > }

    >
    > > }
    > > s1[j] = '\0';

    >
    > > }
    > > //printf("s1:%c",s1[0]);

    >
    > > }
    > > /*>*///end-squeeze

    >
    > > I use squeeze("abcdefb","ab") to call the function.

    >
    > You pass the string literal `"abcdefb"` as the value for the
    > argument `s1`.
    >
    > You are not allowed to write into string literals.
    >
    > You were lucky: BOOM.
    >
    > > Any suggestion?

    >
    > Don't write into string literals, and don't use so many blank lines
    > and whitespace in your code.
    >
    > void squeeze(char s1[],char s2[])
    > {
    > int i,j,k;
    > for(i = 0; s2 !='\0'; ++i)
    > {
    > char de_s = s2;
    > for(j = k = 0; s1[j] != '\0'; ++j)
    > {
    > if(s1[j] != de_s) s1[k++] = s1[j];
    > }
    > s1[j] = '\0';
    > }
    > //printf("s1:%c",s1[0]);
    >
    > }
    >
    > I won't comment on the algorithm itself.
    >
    > --
    > "Possibly you're not recalling some of his previous plans." Zoe, /Firefly/
    >
    > Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    > registered office: Berks RG12 1HN 690597 England


    Thanks a lot!
    I choose let
    char s1[] = "abcdefb";
    char s2[] = "ab";
    squeeze(s1,s2);
    It becomes right,it does the string literals problem.
    May be I should use "temp" stand for "de_s"
    and thank you for the advice
     
    , Nov 7, 2008
    #5
  6. In article <gf18ag$dif$> Chris Dollin <> writes:
    ....
    > void squeeze(char s1[],char s2[])
    > {
    > int i,j,k;
    > for(i = 0; s2 !='\0'; ++i)
    > {
    > char de_s = s2;
    > for(j = k = 0; s1[j] != '\0'; ++j)
    > {
    > if(s1[j] != de_s) s1[k++] = s1[j];
    > }
    > s1[j] = '\0';


    I would make that s1[k] = '\0';
    > }
    > //printf("s1:%c",s1[0]);
    > }

    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Nov 7, 2008
    #6
  7. Guest

    On Nov 7, 9:31 pm, "Dik T. Winter" <> wrote:
    > In article <gf18ag$> Chris Dollin <> writes:
    >
    > ...
    > > void squeeze(char s1[],char s2[])
    > > {
    > > int i,j,k;
    > > for(i = 0; s2 !='\0'; ++i)
    > > {
    > > char de_s = s2;
    > > for(j = k = 0; s1[j] != '\0'; ++j)
    > > {
    > > if(s1[j] != de_s) s1[k++] = s1[j];
    > > }
    > > s1[j] = '\0';

    >
    > I would make that s1[k] = '\0';
    > > }
    > > //printf("s1:%c",s1[0]);
    > > }

    > --
    > dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    > home: bovenover 215, 1025 jn amsterdam, nederland;http://www.cwi.nl/~dik/


    Yeh,you are right ,I have change that expression ,it should be s1[k] =
    '\0'
     
    , Nov 7, 2008
    #7
  8. wrote:
    > void squeeze(char s1[],char s2[])

    [...modifying algorithm on s1...]
    > Thanks a lot!
    > I choose let
    > char s1[] = "abcdefb";
    > char s2[] = "ab";
    > squeeze(s1,s2);


    Actually, you have the program running, but I'm afraid you haven't
    understood the nature of the problem completely yet.

    Firstly, the function

    void squeeze(char s1[],char s2[]);

    is actually a kind of a lie. The point is that there are no arrays passed to
    that function. Instead, this is just a weird way of writing this:

    void squeeze( char* s1, char* s2);

    Now, since you only want to modify what s1 points to but not what s2 points
    to, you can tell that to the compiler:

    void squeeze( char* s1, char const* s2);
    void squeeze( char* s1, const char* s2);

    This allows the compiler to tell you when you modify a char that you
    previously promised not to modify.

    Note that both variants are equivalent. I prefer the first one, because
    putting const to the right of what is const generally works, to the left
    only in one case. You will see both variants in the wild though.

    So, you can now safely invoke the function like

    char s1[] = "abcdef";
    squeeze( s1, "ab");

    Note that here, other than in the function declaration, you can _not_ write

    char* s1 = "abcdef";

    This would compile though, unfortunately. In old C, there was no 'const', so
    this had to compile. In current C, it is still accepted for backward
    compatibility. Take the habit of not using that though.

    Uli
     
    Ulrich Eckhardt, Nov 8, 2008
    #8
    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. Harvey Twyman
    Replies:
    8
    Views:
    582
    August Derleth
    Oct 25, 2003
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,328
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  3. ankur
    Replies:
    1
    Views:
    12,975
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  4. nelson
    Replies:
    3
    Views:
    324
    Piet van Oostrum
    Dec 2, 2010
  5. AlexHWGUY
    Replies:
    5
    Views:
    173
    John W. Krahn
    Oct 24, 2006
Loading...

Share This Page