English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

I wrote this program, it does successfully compile but when running it hangs. and i think the prob must be something like wrong use of function.

#include
#include

void sp_to_dash(char *str);
char s;
int main(void){
printf("Please Enter a expression: \n");
scanf("%c",s);
sp_to_dash("s");
system("pause");
}
void sp_to_dash(char *str){
while (*str)
{
if (*str==' ') printf("%c",'_');
else printf("%c",*str);
str++;
}
}

2007-04-05 11:03:51 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

sp_to_dash(s); doesn't work too

2007-04-05 11:52:48 · update #1

4 answers

sp_to_dash("s");

You are passing the literal value string "S" to the function, BTW... probably not your intention. try sp_to_dash(s).

also, you really don't have anything to terminate your loop. Especially when sending in a literal. The program will just happily keep skipping through memory until it randomly and accidentally runs into a NULL somewhere.

You need to fix the parameter pass as well as add code to make sure that your input string is ALWAYS null terminated...or else check for the str = length of str also in your loop control.

2007-04-05 11:53:02 · answer #1 · answered by tallcowboy0614 6 · 0 0

"s" is defined as a single character, so when you go into the sp_to_dash() routine, you send the address of "s". then you do something then increment the pointer. the problem is the next address location is just garbage because you are only dealing with 1 character. also the line" while(*str) seems to me to be an infinite loop.

2007-04-06 09:05:03 · answer #2 · answered by justme 7 · 0 0

while (*str)

This condition may never be false if str is not null-terminated.

2007-04-05 18:41:00 · answer #3 · answered by icnintel 4 · 0 0

Why are you passing "s" to your function? Shouldn't you be passing your string you just got from scanf?

2007-04-05 18:45:29 · answer #4 · answered by Nisovin 5 · 0 0

fedest.com, questions and answers