segmentation fault

D

Dennis Schulz

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
 
K

Karthik

Dennis said:
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
 
B

Barry Schwarz

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>>
 
T

Thomas Matthews

Dennis said:
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
 
E

Eric Sosman

Mike said:
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);
 
J

Joona I Palaste

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.
 
J

Joona I Palaste

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.
 
M

Mike Wahler

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
 
C

Christopher Benson-Manica

Joona I Palaste said:
"" 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!
 
M

Mike Wahler

Joona I Palaste said:
Christopher Benson-Manica <[email protected]> scribbled the following:
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
 
E

Eric Sosman

Mike said:
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
 
J

Jim

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.

} //end soundex


int main() {

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


soundex(wort, &ausgabe);

} // END MAIN

Jim
 
B

Bill Cunningham

Jim said:
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.

} //end soundex


int main() {

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


soundex(wort, &ausgabe);

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top