为什么这次撞车? [英] why this crash?

查看:53
本文介绍了为什么这次撞车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这可能是一个非常简单的问题,但是我看不到它......


这是代码:


char * lower(char * str){

char * p = str;

while(* str){

* str = tolower(* str); < =====崩溃!

str ++;

}

返回p;

}


你明白为什么吗?

Hi,
It could be a very simple problem, but i can not see it...

Here is the code:

char* lower(char* str){
char* p = str;
while ( *str ) {
*str = tolower(*str); <=====crash!
str++;
}
return p;
}

DO you see why?

推荐答案

li ***** @ hotmail.com 写道:



这可能是一个非常简单的问题,但是我看不到它......


这是代码:


char * lower (char * str){

char * p = str;

while(* str){

* str = tolower(* str) ; < =====崩溃!

str ++;

}

返回p;

}
Hi,
It could be a very simple problem, but i can not see it...

Here is the code:

char* lower(char* str){
char* p = str;
while ( *str ) {
*str = tolower(*str); <=====crash!
str++;
}
return p;
}



删除< ===== crash!后:


test.c:在函数中?? lowera ??:

test.c:4:警告:隐式声明函数a ?? tolowera ??

/ usr / lib / gcc / x86_64-pc -linux-gnu / 4.1.2 /../../../../ lib64 / crt1.o:在功能中

`_start'':

(.text + 0x20):对'main'的未定义引用'

collect2:ld返回1退出状态


如果可能,请发布完整的可编译程序。

After removing the "<=====crash!":

test.c: In function a??lowera??:
test.c:4: warning: implicit declaration of function a??tolowera??
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/../../../../lib64/crt1.o: In function
`_start'':
(.text+0x20): undefined reference to `main''
collect2: ld returned 1 exit status

Please, when possible, post complete compilable programs.


你明白为什么吗?
DO you see why?



您还没有发布足够的代码来说明这一点。一个可能的可能性

在常见问题解答中解释为< http://c-faq.com/decl/strlitinit.html>,但

还有其他可能性。如果发生了这种情况,那么问题

不在您发布的代码中,而是在您调用较低功能的方式中。

如果您发布了完整的可编辑内容程序,如果发生这种情况,可能会确定


You have not posted enough code to make this clear. One likely possibility
is explained in the FAQ at <http://c-faq.com/decl/strlitinit.html>, but
there are other possibilities. If this is what happened, then the problem
is not in the code that you posted, but in how you call the lower function.
If you had posted a complete compilable program, it would have been
possible to be sure if this is what happened.


li ***** @ hotmail.com 写道:

这可能是一个非常简单的问题,但我看不到它......


这是代码:


char * lower(char * str){

char * p = str;

while(* str){

* str = tolower(* str); < =====崩溃!

str ++;

}

返回p;

}


你明白为什么吗?
It could be a very simple problem, but i can not see it...

Here is the code:

char* lower(char* str){
char* p = str;
while ( *str ) {
*str = tolower(*str); <=====crash!
str++;
}
return p;
}

DO you see why?



不,因为你没有发布整个程序,所以我们看不到

实际错误在哪里。


但是我注意到如果你用一个字符串

literal作为参数调用你的lower()函数,很可能发生崩溃。


您还应该小心传递给tolower()的参数。除EOF之外的
负值会导致未定义的行为;如果您的系统上签署了普通字符
,那也可能导致崩溃。


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

No, because you didn''t post the entire program, so we can''t see where
the actual error is.

But I note that if you call your lower() function with a string
literal as the argument, a crash is very likely.

You should also be careful with the argument you pass to tolower(). A
negative value other than EOF causes undefined behavior; if plain char
is signed on your system, that could also cause a crash.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


7月3日,5日:37 am,linq ... @ hotmail.com写道:
On Jul 3, 5:37 am, linq...@hotmail.com wrote:



这可能是一个非常简单的问题,但我看不到它......


这是代码:


char * lower(char * str){

char * p = str;

while(* str){

* str = tolower(* str); < =====崩溃!

str ++;

}

返回p;


}


你明白为什么吗?
Hi,
It could be a very simple problem, but i can not see it...

Here is the code:

char* lower(char* str){
char* p = str;
while ( *str ) {
*str = tolower(*str); <=====crash!
str++;
}
return p;

}

DO you see why?



我不确定,但我认为该参数应该像

char * str而不是char * str一样传递。如果我错了,请及时纠正我,还请

请告诉我两者之间的区别。

谢谢

I am not sure but I think the argument is supposed to be passed like
char *str and not char* str. If I am wrong kindly correct me and also
please tell me the difference between the two.
Thank you


这篇关于为什么这次撞车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆