Connecting Tech Pros Worldwide Forums | Help | Site Map

segmentation fault

Dennis Schulz
Guest
 
Posts: n/a
#1: Nov 14 '05
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

Karthik
Guest
 
Posts: n/a
#2: Nov 14 '05

re: segmentation fault


Dennis Schulz wrote:
[color=blue]
> 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;[/color]
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. ..
[color=blue]
> 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;[/color]

In C, try to group all the declarations in the beginning of the
function.
[color=blue]
> sprintf(zifferAlsString, "%d", ziffer);[/color]

How did this compile in the first place ? zifflerAsString is an
'int' . But the first arg of sprintf ought to be a char *.
[color=blue]
> // letztes zeichen in res:
> char lastchar = res[strlen(res)-1];[/color]
" In C, try to group all the declarations in the beginning of the
function. "
[color=blue]
> //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');[/color]
Did u mean strcat(res,'\0'); ?
[color=blue]
> } // 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);[/color]

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.
Barry Schwarz
Guest
 
Posts: n/a
#3: Nov 14 '05

re: segmentation fault


On 4 May 2004 17:02:53 -0700, d.schulz81@gmx.net (Dennis Schulz)
wrote:
[color=blue]
>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[/color]

Your code has numerous syntax errors. You should set the warning
level to maximum and not execute until you get a clean compile.
[color=blue]
>
>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;[/color]

C89 requires all declarations to appear before executable code. Move
this up two lines.
[color=blue]
> 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);[/color]

Syntax error. The first argument to sprintf must be char*, not an int
as you have it.
[color=blue]
> // letztes zeichen in res:
> char lastchar = res[strlen(res)-1];
> //wenn ziffer ungleich letztem zeichen in res
> if (ziffer != lastchar) {
> strcat(res, zifferAlsString);[/color]

Ditto.
[color=blue]
> }
> //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');[/color]

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.
[color=blue]
> } // 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) {[/color]

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...
[color=blue]
>
>
> soundex(wort, &ausgabe);[/color]

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.
[color=blue]
> printf("%s", "Der Code ist :");
> printf("%s", ausgabe);
> printf("\n");
> } // end while
>
>} // END MAIN[/color]



<<Remove the del for email>>
Mike Wahler
Guest
 
Posts: n/a
#4: Nov 14 '05

re: segmentation fault


"Karthik" <removeme_kaykaydreamz@yahoo.com> wrote in message
news:40984ce5$1@darkstar...[color=blue]
> Dennis Schulz wrote:[color=green]
> > for(k = (strlen(res)-1); k<7; k++) {
> > strcat(res,'0');[/color]
> Did u mean strcat(res,'\0'); ?[/color]

Both params are pointers.

strcat(rec, "\0");

-Mike


Karthik
Guest
 
Posts: n/a
#5: Nov 14 '05

re: segmentation fault


Mike Wahler wrote:
[color=blue]
> "Karthik" <removeme_kaykaydreamz@yahoo.com> wrote in message
> news:40984ce5$1@darkstar...
>[color=green]
>>Dennis Schulz wrote:
>>[color=darkred]
>>>for(k = (strlen(res)-1); k<7; k++) {
>>>strcat(res,'0');[/color]
>>
>>Did u mean strcat(res,'\0'); ?[/color]
>
>
> Both params are pointers.
>
> strcat(rec, "\0");
>
> -Mike
>
>[/color]
Oh !! Oops !!

Thanks for the correction , Mike.

--
Karthik.
Humans please 'removeme_' for my real email.
Thomas Matthews
Guest
 
Posts: n/a
#6: Nov 14 '05

re: segmentation fault


Dennis Schulz wrote:[color=blue]
> 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;[/color]

I'm not performing a syntax check, as other people have
spotted these errors.
[color=blue]
> while(strlen(res)< 7) {
> buchstabe = toupper(s[j]);
> switch (buchstabe) {
> case 'B':
> case 'F':
> case 'P':
> case 'V':
> ziffer = 1;
> break;[/color]

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;
}
[color=blue]
> case 'C':
> case 'K':
> case 'Q':
> case 'J':
> case 'G':
> case 'S':
> case 'X':
> case 'Z':
> ziffer = 2;
> break;[/color]

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;
}
}

[color=blue]
> case 'D':
> case 'T':
> ziffer = 3;
> break;
>
> case 'L':
> ziffer = 4;
> break;
>
> case 'M':
> case 'N':
> ziffer = 5;
> break;
> case 'R':
> ziffer = 6;
> break;
> }[/color]

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

Eric Sosman
Guest
 
Posts: n/a
#7: Nov 14 '05

re: segmentation fault


Mike Wahler wrote:[color=blue]
>
> "Karthik" <removeme_kaykaydreamz@yahoo.com> wrote in message
> news:40984ce5$1@darkstar...[color=green]
> > Dennis Schulz wrote:[color=darkred]
> > > for(k = (strlen(res)-1); k<7; k++) {
> > > strcat(res,'0');[/color]
> > Did u mean strcat(res,'\0'); ?[/color]
>
> Both params are pointers.
>
> strcat(rec, "\0");[/color]

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@sun.com
Joona I Palaste
Guest
 
Posts: n/a
#8: Nov 14 '05

re: segmentation fault


Eric Sosman <Eric.Sosman@sun.com> scribbled the following:[color=blue]
> Mike Wahler wrote:[color=green]
>> "Karthik" <removeme_kaykaydreamz@yahoo.com> wrote in message
>> news:40984ce5$1@darkstar...[color=darkred]
>> > Dennis Schulz wrote:
>> > > for(k = (strlen(res)-1); k<7; k++) {
>> > > strcat(res,'0');
>> > Did u mean strcat(res,'\0'); ?[/color]
>>
>> Both params are pointers.
>>
>> strcat(rec, "\0");[/color][/color]
[color=blue]
> Assuming `res' is a proper string pointer, this is
> a no-op. Here are some additional ways of spelling the
> same no-op:[/color]
[color=blue]
> strcat(rec, "\0\0\0\0\0\0\0");
> strcat(rec, "\0Hello, world\n");
> strcat(rec, "");
> strcat, ("", rec);[/color]

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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
Christopher Benson-Manica
Guest
 
Posts: n/a
#9: Nov 14 '05

re: segmentation fault


Eric Sosman <Eric.Sosman@sun.com> spoke thus:
[color=blue]
> strcat, ("", rec);[/color]

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.
Joona I Palaste
Guest
 
Posts: n/a
#10: Nov 14 '05

re: segmentation fault


Christopher Benson-Manica <ataru@nospam.cyberspace.org> scribbled the following:[color=blue]
> Eric Sosman <Eric.Sosman@sun.com> spoke thus:[color=green]
>> strcat, ("", rec);[/color][/color]
[color=blue]
> Surely "" doesn't count as a char*? If this is legal, it's news to me...[/color]

"" 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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"That's no raisin - it's an ALIEN!"
- Tourist in MTV's Oddities
Mike Wahler
Guest
 
Posts: n/a
#11: Nov 14 '05

re: segmentation fault



"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c7batg$jnb$2@chessie.cirr.com...[color=blue]
> Eric Sosman <Eric.Sosman@sun.com> spoke thus:
>[color=green]
> > strcat, ("", rec);[/color][/color]

I'll assume that comma after the function name to
be a typo.
[color=blue]
>
> Surely "" doesn't count as a char*?[/color]

Yes, that's its type.
[color=blue]
> If this is legal, it's news to me...[/color]

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


Christopher Benson-Manica
Guest
 
Posts: n/a
#12: Nov 14 '05

re: segmentation fault


Joona I Palaste <palaste@cc.helsinki.fi> spoke thus:
[color=blue]
> "" 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.[/color]

I knew that; I was referring to the fact that it was a literal...
[color=blue]
> 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.[/color]

....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.
Mike Wahler
Guest
 
Posts: n/a
#13: Nov 14 '05

re: segmentation fault



"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:c7bbrt$95j$1@oravannahka.helsinki.fi...[color=blue]
> Christopher Benson-Manica <ataru@nospam.cyberspace.org> scribbled the[/color]
following:[color=blue][color=green]
> > Eric Sosman <Eric.Sosman@sun.com> spoke thus:[color=darkred]
> >> strcat, ("", rec);[/color][/color]
>[color=green]
> > Surely "" doesn't count as a char*? If this is legal, it's news to[/color][/color]
me...[color=blue]
>
> "" 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.[/color]

So the comma was intentional?

-Mike


Eric Sosman
Guest
 
Posts: n/a
#14: Nov 14 '05

re: segmentation fault


Mike Wahler wrote:[color=blue]
>
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
> news:c7bbrt$95j$1@oravannahka.helsinki.fi...[color=green]
> > Christopher Benson-Manica <ataru@nospam.cyberspace.org> scribbled the[/color]
> following:[color=green][color=darkred]
> > > Eric Sosman <Eric.Sosman@sun.com> spoke thus:
> > >> strcat, ("", rec);[/color]
> >[color=darkred]
> > > Surely "" doesn't count as a char*? If this is legal, it's news to[/color][/color]
> me...[color=green]
> >
> > "" 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.[/color]
>
> So the comma was intentional?[/color]

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

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

--
Eric.Sosman@sun.com
Jim
Guest
 
Posts: n/a
#15: Nov 14 '05

re: segmentation fault


On 4 May 2004 17:02:53 -0700, d.schulz81@gmx.net (Dennis Schulz)
wrote:
[color=blue]
>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) {[/color]

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

<snip>
[color=blue]
>} //end soundex
>
>
>int main() {[/color]

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

<snip>
[color=blue]
>} // END MAIN[/color]

Jim
Bill Cunningham
Guest
 
Posts: n/a
#16: Nov 14 '05

re: segmentation fault



"Jim" <spam@ihug.com.au> wrote in message
news:0e0j90lonr6nqfdrbmm0thohqvvslouh9q@4ax.com...[color=blue]
> On 4 May 2004 17:02:53 -0700, d.schulz81@gmx.net (Dennis Schulz)
> wrote:
>[color=green]
> >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) {[/color]
>
> I cannot see where 'res' is 0 terminated, so this will invoke UB.
>
> <snip>
>[color=green]
> >} //end soundex
> >
> >
> >int main() {[/color]
>
> <snip>
>[color=green]
> > char ausgabe[1000];
> > printf("%s", "Bitte Eingabe treffen: " );
> > printf("\n");
> > while(scanf("%s", &wort) != EOF) {
> >
> >
> > soundex(wort, &ausgabe);[/color]
>
> <snip>
>[color=green]
> >} // END MAIN[/color]
>
> Jim[/color]

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



Closed Thread