segmentation fault

Discussion in 'C Programming' started by Dennis Schulz, May 5, 2004.

  1. hi y'all

    im a beginner in C language and i have problems running this programm.
    when i enter a newline there is a "segmentation fault". i dont know
    what it means and already spent too much time looking for the reason.
    maybe (hopefully) you can help me. Dennis

    Coding:

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

    void soundex(char *s, char *res) {
    char buchstabe;
    int ziffer;
    res[0] = toupper(s[0]);
    //zaehler
    int j=0;
    while(strlen(res)< 7) {
    buchstabe = toupper(s[j]);
    switch (buchstabe) {
    case 'B':
    case 'F':
    case 'P':
    case 'V':
    ziffer = 1;
    break;
    case 'C':
    case 'K':
    case 'Q':
    case 'J':
    case 'G':
    case 'S':
    case 'X':
    case 'Z':
    ziffer = 2;
    break;
    case 'D':
    case 'T':
    ziffer = 3;
    break;

    case 'L':
    ziffer = 4;
    break;

    case 'M':
    case 'N':
    ziffer = 5;
    break;
    case 'R':
    ziffer = 6;
    break;
    }
    int zifferAlsString;
    sprintf(zifferAlsString, "%d", ziffer);
    // letztes zeichen in res:
    char lastchar = res[strlen(res)-1];
    //wenn ziffer ungleich letztem zeichen in res
    if (ziffer != lastchar) {
    strcat(res, zifferAlsString);
    }
    //zaehler erhoehen
    j++;
    } // end while
    if (strlen(res)<7) {
    int k;
    // Rest mit Nullen fuellen
    for(k = (strlen(res)-1); k<7; k++) {
    strcat(res,'0');
    } // end for
    } // end if
    // trennzeichen hin
    strcat(res,'\0');

    } //end soundex


    int main() {

    /* lese zeichenkette ein */

    char wort[1000];
    char ausgabe[1000];
    printf("%s", "Bitte Eingabe treffen: " );
    printf("\n");
    while(scanf("%s", &wort) != EOF) {


    soundex(wort, &ausgabe);
    printf("%s", "Der Code ist :");
    printf("%s", ausgabe);
    printf("\n");
    } // end while

    } // END MAIN
     
    Dennis Schulz, May 5, 2004
    #1
    1. Advertising

  2. Dennis Schulz

    Karthik Guest

    Dennis Schulz wrote:

    > hi y'all
    >
    > im a beginner in C language and i have problems running this programm.
    > when i enter a newline there is a "segmentation fault". i dont know
    > what it means and already spent too much time looking for the reason.
    > maybe (hopefully) you can help me. Dennis
    >
    > Coding:
    >
    > #include <stdio.h>
    > #include <ctype.h>
    > #include <string.h>
    >
    > void soundex(char *s, char *res) {
    > char buchstabe;
    > int ziffer;
    > res[0] = toupper(s[0]);
    > //zaehler
    > int j=0;

    This is not strictly C. May be C++ compiler. When posting to the
    group, please post strict C code so that it helps us to paste it ,
    compile and check the error. ..

    > while(strlen(res)< 7) {
    > buchstabe = toupper(s[j]);
    > switch (buchstabe) {
    > case 'B':
    > case 'F':
    > case 'P':
    > case 'V':
    > ziffer = 1;
    > break;
    > case 'C':
    > case 'K':
    > case 'Q':
    > case 'J':
    > case 'G':
    > case 'S':
    > case 'X':
    > case 'Z':
    > ziffer = 2;
    > break;
    > case 'D':
    > case 'T':
    > ziffer = 3;
    > break;
    >
    > case 'L':
    > ziffer = 4;
    > break;
    >
    > case 'M':
    > case 'N':
    > ziffer = 5;
    > break;
    > case 'R':
    > ziffer = 6;
    > break;
    > }
    > int zifferAlsString;


    In C, try to group all the declarations in the beginning of the
    function.

    > sprintf(zifferAlsString, "%d", ziffer);


    How did this compile in the first place ? zifflerAsString is an
    'int' . But the first arg of sprintf ought to be a char *.

    > // letztes zeichen in res:
    > char lastchar = res[strlen(res)-1];

    " In C, try to group all the declarations in the beginning of the
    function. "

    > //wenn ziffer ungleich letztem zeichen in res
    > if (ziffer != lastchar) {
    > strcat(res, zifferAlsString);
    > }
    > //zaehler erhoehen
    > j++;
    > } // end while
    > if (strlen(res)<7) {
    > int k;
    > // Rest mit Nullen fuellen
    > for(k = (strlen(res)-1); k<7; k++) {
    > strcat(res,'0');

    Did u mean strcat(res,'\0'); ?

    > } // end for
    > } // end if
    > // trennzeichen hin
    > strcat(res,'\0');
    >
    > } //end soundex
    >
    >
    > int main() {
    >
    > /* lese zeichenkette ein */
    >
    > char wort[1000];
    > char ausgabe[1000];
    > printf("%s", "Bitte Eingabe treffen: " );
    > printf("\n");
    > while(scanf("%s", &wort) != EOF) {
    >
    >
    > soundex(wort, &ausgabe);


    soundex(wort, ausgabe);
    address-of operator not needed here .. ausgabe anyway represents the
    address-of the first element in the array that you had declared.

    HTH

    --
    Karthik.
    Humans please 'removeme_' for my real email.
     
    Karthik, May 5, 2004
    #2
    1. Advertising

  3. On 4 May 2004 17:02:53 -0700, (Dennis Schulz)
    wrote:

    >hi y'all
    >
    >im a beginner in C language and i have problems running this programm.
    >when i enter a newline there is a "segmentation fault". i dont know
    >what it means and already spent too much time looking for the reason.
    >maybe (hopefully) you can help me. Dennis


    Your code has numerous syntax errors. You should set the warning
    level to maximum and not execute until you get a clean compile.

    >
    >Coding:
    >
    >#include <stdio.h>
    >#include <ctype.h>
    >#include <string.h>
    >
    >void soundex(char *s, char *res) {
    > char buchstabe;
    > int ziffer;
    > res[0] = toupper(s[0]);
    > //zaehler
    > int j=0;


    C89 requires all declarations to appear before executable code. Move
    this up two lines.

    > while(strlen(res)< 7) {
    > buchstabe = toupper(s[j]);
    > switch (buchstabe) {
    > case 'B':
    > case 'F':
    > case 'P':
    > case 'V':
    > ziffer = 1;
    > break;
    > case 'C':
    > case 'K':
    > case 'Q':
    > case 'J':
    > case 'G':
    > case 'S':
    > case 'X':
    > case 'Z':
    > ziffer = 2;
    > break;
    > case 'D':
    > case 'T':
    > ziffer = 3;
    > break;
    >
    > case 'L':
    > ziffer = 4;
    > break;
    >
    > case 'M':
    > case 'N':
    > ziffer = 5;
    > break;
    > case 'R':
    > ziffer = 6;
    > break;
    > }
    > int zifferAlsString;
    > sprintf(zifferAlsString, "%d", ziffer);


    Syntax error. The first argument to sprintf must be char*, not an int
    as you have it.

    > // letztes zeichen in res:
    > char lastchar = res[strlen(res)-1];
    > //wenn ziffer ungleich letztem zeichen in res
    > if (ziffer != lastchar) {
    > strcat(res, zifferAlsString);


    Ditto.

    > }
    > //zaehler erhoehen
    > j++;
    > } // end while
    > if (strlen(res)<7) {
    > int k;
    > // Rest mit Nullen fuellen
    > for(k = (strlen(res)-1); k<7; k++) {
    > strcat(res,'0');


    What are you trying to do here. If you intending to add zeros to your
    string, you need "0", not '0'. Suggest you also look at strncpy.

    > } // end for
    > } // end if
    > // trennzeichen hin
    > strcat(res,'\0');
    >
    >} //end soundex
    >
    >
    >int main() {
    >
    > /* lese zeichenkette ein */
    >
    > char wort[1000];
    > char ausgabe[1000];
    > printf("%s", "Bitte Eingabe treffen: " );
    > printf("\n");
    > while(scanf("%s", &wort) != EOF) {


    The & here is wrong but probably does not cause the problem you are
    experiencing. The %s format requires that the corresponding argument
    be a char*. &wort is not a char*; it is a char (*)[1000]. On most
    common systems, the two types have the same representation. But...

    >
    >
    > soundex(wort, &ausgabe);


    This is a syntax error which your compiler should have diagnosed. The
    second argument to soundex must be a char*. &ausgabe has type char
    (*)[1000] which is not the same or compatible. If you did not receive
    a diagnostic when compiling this, up the warning level of your
    compiler.

    > printf("%s", "Der Code ist :");
    > printf("%s", ausgabe);
    > printf("\n");
    > } // end while
    >
    >} // END MAIN




    <<Remove the del for email>>
     
    Barry Schwarz, May 5, 2004
    #3
  4. Dennis Schulz

    Mike Wahler Guest

    "Karthik" <> wrote in message
    news:40984ce5$1@darkstar...
    > Dennis Schulz wrote:
    > > for(k = (strlen(res)-1); k<7; k++) {
    > > strcat(res,'0');

    > Did u mean strcat(res,'\0'); ?


    Both params are pointers.

    strcat(rec, "\0");

    -Mike
     
    Mike Wahler, May 5, 2004
    #4
  5. Dennis Schulz

    Karthik Guest

    Mike Wahler wrote:

    > "Karthik" <> wrote in message
    > news:40984ce5$1@darkstar...
    >
    >>Dennis Schulz wrote:
    >>
    >>>for(k = (strlen(res)-1); k<7; k++) {
    >>>strcat(res,'0');

    >>
    >>Did u mean strcat(res,'\0'); ?

    >
    >
    > Both params are pointers.
    >
    > strcat(rec, "\0");
    >
    > -Mike
    >
    >

    Oh !! Oops !!

    Thanks for the correction , Mike.

    --
    Karthik.
    Humans please 'removeme_' for my real email.
     
    Karthik, May 5, 2004
    #5
  6. Dennis Schulz wrote:
    > hi y'all
    >
    > im a beginner in C language and i have problems running this programm.
    > when i enter a newline there is a "segmentation fault". i dont know
    > what it means and already spent too much time looking for the reason.
    > maybe (hopefully) you can help me. Dennis
    >
    > Coding:
    >
    > #include <stdio.h>
    > #include <ctype.h>
    > #include <string.h>
    >
    > void soundex(char *s, char *res) {
    > char buchstabe;
    > int ziffer;
    > res[0] = toupper(s[0]);
    > //zaehler
    > int j=0;


    I'm not performing a syntax check, as other people have
    spotted these errors.

    > while(strlen(res)< 7) {
    > buchstabe = toupper(s[j]);
    > switch (buchstabe) {
    > case 'B':
    > case 'F':
    > case 'P':
    > case 'V':
    > ziffer = 1;
    > break;


    Another suggestion is to place the letters into a
    constant string and search the string:
    const char * ziffer1_buchstabe[] = "BFPV"
    /*...*/
    if (strchr(ziffer1_buchstabe, buchstabe) != NULL)
    {
    ziffer = 1;
    }

    > case 'C':
    > case 'K':
    > case 'Q':
    > case 'J':
    > case 'G':
    > case 'S':
    > case 'X':
    > case 'Z':
    > ziffer = 2;
    > break;


    Compilers will have an easier time optimizing the
    case if the selectors are ordered.

    Also, one could use a table of <char *, ziffer numer>.
    If the buchstabe is in the string, use the associated
    ziffer numer.
    struct buchstabe_record
    {
    const char * choices;
    unsigned int ziffer_numer;
    };
    const buchstabe_record buchstabe_tisch[] =
    {
    {"BFPV", 1},
    {"CGJKQSXZ", 2},
    /* u.s.v. */
    };
    const unsigned int NUMER_RECORDS =
    sizeof(buchstabe_tisch) / sizeof(buchstabe_tisch[0]);
    /* ... */
    for (index = 0; index < NUMER_RECORDS; ++index)
    {
    if (strchr(buchstabe_tisch[index].choices, buchstabe)
    != NULL)
    {
    ziffer = buchstabe_tisch[index].ziffer_numer;
    break;
    }
    }


    > case 'D':
    > case 'T':
    > ziffer = 3;
    > break;
    >
    > case 'L':
    > ziffer = 4;
    > break;
    >
    > case 'M':
    > case 'N':
    > ziffer = 5;
    > break;
    > case 'R':
    > ziffer = 6;
    > break;
    > }


    Also, you should use the "default" case too.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, May 5, 2004
    #6
  7. Dennis Schulz

    Eric Sosman Guest

    Mike Wahler wrote:
    >
    > "Karthik" <> wrote in message
    > news:40984ce5$1@darkstar...
    > > Dennis Schulz wrote:
    > > > for(k = (strlen(res)-1); k<7; k++) {
    > > > strcat(res,'0');

    > > Did u mean strcat(res,'\0'); ?

    >
    > Both params are pointers.
    >
    > strcat(rec, "\0");


    Assuming `res' is a proper string pointer, this is
    a no-op. Here are some additional ways of spelling the
    same no-op:

    strcat(rec, "\0\0\0\0\0\0\0");
    strcat(rec, "\0Hello, world\n");
    strcat(rec, "");
    strcat, ("", rec);

    --
     
    Eric Sosman, May 5, 2004
    #7
  8. Eric Sosman <> scribbled the following:
    > Mike Wahler wrote:
    >> "Karthik" <> wrote in message
    >> news:40984ce5$1@darkstar...
    >> > Dennis Schulz wrote:
    >> > > for(k = (strlen(res)-1); k<7; k++) {
    >> > > strcat(res,'0');
    >> > Did u mean strcat(res,'\0'); ?

    >>
    >> Both params are pointers.
    >>
    >> strcat(rec, "\0");


    > Assuming `res' is a proper string pointer, this is
    > a no-op. Here are some additional ways of spelling the
    > same no-op:


    > strcat(rec, "\0\0\0\0\0\0\0");
    > strcat(rec, "\0Hello, world\n");
    > strcat(rec, "");
    > strcat, ("", rec);


    I was thinking pretty much the same thing. Of course, as we all know,
    "" and "\0" are not *quite* the same thing, as the first consists of one
    0 byte but the second of two. So how can strcat() know which we mean?
    The answer: short of mind-reading, it can't. Neither can pretty much any
    other str*() function. If you wish to differentiate between the two, you
    must come up with your own string functions.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I will never display my bum in public again."
    - Homer Simpson
     
    Joona I Palaste, May 5, 2004
    #8
  9. Eric Sosman <> spoke thus:

    > strcat, ("", rec);


    Surely "" doesn't count as a char*? If this is legal, it's news to me...

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, May 5, 2004
    #9
  10. Christopher Benson-Manica <> scribbled the following:
    > Eric Sosman <> spoke thus:
    >> strcat, ("", rec);


    > Surely "" doesn't count as a char*? If this is legal, it's news to me...


    "" counts as a char* like any other string does. It's a char* pointing
    to a char that has the value 0, and the subsequent chars can have any
    value they bloody well want.
    At first Eric's code might seem like UB waiting to happen, but note the
    sneaky syntax: it's actually a no-op. It evaluates strcat, then
    evaluates "" and finally evaluates rec, and that's all it does.
    By "evaluates strcat" I mean evaluating the address of the function
    strcat. It does not call this function at all.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "That's no raisin - it's an ALIEN!"
    - Tourist in MTV's Oddities
     
    Joona I Palaste, May 5, 2004
    #10
  11. Dennis Schulz

    Mike Wahler Guest

    "Christopher Benson-Manica" <> wrote in message
    news:c7batg$jnb$...
    > Eric Sosman <> spoke thus:
    >
    > > strcat, ("", rec);


    I'll assume that comma after the function name to
    be a typo.

    >
    > Surely "" doesn't count as a char*?


    Yes, that's its type.

    > If this is legal, it's news to me...


    The above isn't legal because it attempts to modify
    a literal. Swap the arguments, and it's fine (but
    is effectively a no-op).

    -Mike
     
    Mike Wahler, May 5, 2004
    #11
  12. Joona I Palaste <> spoke thus:

    > "" counts as a char* like any other string does. It's a char* pointing
    > to a char that has the value 0, and the subsequent chars can have any
    > value they bloody well want.


    I knew that; I was referring to the fact that it was a literal...

    > At first Eric's code might seem like UB waiting to happen, but note the
    > sneaky syntax: it's actually a no-op. It evaluates strcat, then
    > evaluates "" and finally evaluates rec, and that's all it does.
    > By "evaluates strcat" I mean evaluating the address of the function
    > strcat. It does not call this function at all.


    ....because I missed the duplicitous comma that you referred to. Rats!

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, May 5, 2004
    #12
  13. Dennis Schulz

    Mike Wahler Guest

    "Joona I Palaste" <> wrote in message
    news:c7bbrt$95j$...
    > Christopher Benson-Manica <> scribbled the

    following:
    > > Eric Sosman <> spoke thus:
    > >> strcat, ("", rec);

    >
    > > Surely "" doesn't count as a char*? If this is legal, it's news to

    me...
    >
    > "" counts as a char* like any other string does. It's a char* pointing
    > to a char that has the value 0, and the subsequent chars can have any
    > value they bloody well want.
    > At first Eric's code might seem like UB waiting to happen, but note the
    > sneaky syntax: it's actually a no-op. It evaluates strcat, then
    > evaluates "" and finally evaluates rec, and that's all it does.
    > By "evaluates strcat" I mean evaluating the address of the function
    > strcat. It does not call this function at all.


    So the comma was intentional?

    -Mike
     
    Mike Wahler, May 5, 2004
    #13
  14. Dennis Schulz

    Eric Sosman Guest

    Mike Wahler wrote:
    >
    > "Joona I Palaste" <> wrote in message
    > news:c7bbrt$95j$...
    > > Christopher Benson-Manica <> scribbled the

    > following:
    > > > Eric Sosman <> spoke thus:
    > > >> strcat, ("", rec);

    > >
    > > > Surely "" doesn't count as a char*? If this is legal, it's news to

    > me...
    > >
    > > "" counts as a char* like any other string does. It's a char* pointing
    > > to a char that has the value 0, and the subsequent chars can have any
    > > value they bloody well want.
    > > At first Eric's code might seem like UB waiting to happen, but note the
    > > sneaky syntax: it's actually a no-op. It evaluates strcat, then
    > > evaluates "" and finally evaluates rec, and that's all it does.
    > > By "evaluates strcat" I mean evaluating the address of the function
    > > strcat. It does not call this function at all.

    >
    > So the comma was intentional?


    "I meant what I said and I said what I meant."
    -- Horton the Elephant

    "... for a change."
    -- Eric the Fallible

    --
     
    Eric Sosman, May 5, 2004
    #14
  15. Dennis Schulz

    Jim Guest

    On 4 May 2004 17:02:53 -0700, (Dennis Schulz)
    wrote:

    >hi y'all
    >
    >im a beginner in C language and i have problems running this programm.
    >when i enter a newline there is a "segmentation fault". i dont know
    >what it means and already spent too much time looking for the reason.
    >maybe (hopefully) you can help me. Dennis
    >
    >Coding:
    >
    >#include <stdio.h>
    >#include <ctype.h>
    >#include <string.h>
    >
    >void soundex(char *s, char *res) {
    > char buchstabe;
    > int ziffer;
    > res[0] = toupper(s[0]);
    > //zaehler
    > int j=0;
    > while(strlen(res)< 7) {


    I cannot see where 'res' is 0 terminated, so this will invoke UB.

    <snip>

    >} //end soundex
    >
    >
    >int main() {


    <snip>

    > char ausgabe[1000];
    > printf("%s", "Bitte Eingabe treffen: " );
    > printf("\n");
    > while(scanf("%s", &wort) != EOF) {
    >
    >
    > soundex(wort, &ausgabe);


    <snip>

    >} // END MAIN


    Jim
     
    Jim, May 6, 2004
    #15
  16. "Jim" <> wrote in message
    news:...
    > On 4 May 2004 17:02:53 -0700, (Dennis Schulz)
    > wrote:
    >
    > >hi y'all
    > >
    > >im a beginner in C language and i have problems running this programm.
    > >when i enter a newline there is a "segmentation fault". i dont know
    > >what it means and already spent too much time looking for the reason.
    > >maybe (hopefully) you can help me. Dennis
    > >
    > >Coding:
    > >
    > >#include <stdio.h>
    > >#include <ctype.h>
    > >#include <string.h>
    > >
    > >void soundex(char *s, char *res) {
    > > char buchstabe;
    > > int ziffer;
    > > res[0] = toupper(s[0]);
    > > //zaehler
    > > int j=0;
    > > while(strlen(res)< 7) {

    >
    > I cannot see where 'res' is 0 terminated, so this will invoke UB.
    >
    > <snip>
    >
    > >} //end soundex
    > >
    > >
    > >int main() {

    >
    > <snip>
    >
    > > char ausgabe[1000];
    > > printf("%s", "Bitte Eingabe treffen: " );
    > > printf("\n");
    > > while(scanf("%s", &wort) != EOF) {
    > >
    > >
    > > soundex(wort, &ausgabe);

    >
    > <snip>
    >
    > >} // END MAIN

    >
    > Jim


    Not sure. Compile the code from C into assembly and check with an
    assembly language group. Your code may have used a segment register and not
    balanced the stack frame. I better shut up.

    Bill
     
    Bill Cunningham, May 9, 2004
    #16
    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. Alex Hunsley
    Replies:
    17
    Views:
    888
  2. Pud
    Replies:
    0
    Views:
    602
  3. Replies:
    0
    Views:
    557
  4. Ivan Vecerina
    Replies:
    0
    Views:
    500
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,718
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page