string。 Colud有人帮我理解 这段代码有什么问题? 谢谢和最诚挚的问候, CocyHi,This might be a sort of FAQ, but I don''t see why,so I would someone help me to understand what''s wrong?I''ve just created following code which wold trimwhite space(s) in a (given) string.But, it resulted the Segmentation fault, and so aswhen running in gdb (saying "Program received signalSIGSEGV, Segmentaion fault at *p++ = *st++").The platform is Linux kernel 2.4.27, gcc version2.95.4 20011002./*-------------------------------------*/#include <stdio.h>#include <ctype.h>int main(int argc, char ** argv){char *st = "Hey, how are you?";char *p, *s;p = s = st;while( *st ){if ( isspace( (int)*st ) )st++;else*p++ = *st++;}*p = ''/0'';printf("whitespace trimed : %s\n", s);}Compile itself doesn''t complain anything, and I don''tsee what''s wrong with copying the character in thestring to the other place (in memory) where should notoverlap the end of the string (''/0'') of the originalstring.Colud someone help me to understand what''s wrong withthis code?Thanks and Best Regards,Cocy推荐答案 Cocy< ku ********* @ hotmail .COM>这样说:Cocy <ku*********@hotmail.com> spoke thus: char * st ="嘿,你好吗?" ;; char * p,* s; p = s = st ; char *st = "Hey, how are you?"; char *p, *s; p = s = st; p,s和st都指向同一个字符串文字。您可能不会尝试修改字符串文字;这很可能是你麻烦的来源。 < ot>您可以将选项传递给gcc以使代码按原样运行。< ; / ot> - Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我 ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。p, s, and st all point at the same string literal. You may notattempt to modify a string literal; this is most likely the source ofyour troubles.<ot>You can pass an option to gcc to make the code work as is.</ot>--Christopher Benson-Manica | I *should* know what I''m talking about - if Iataru(at)cyberspace.org | don''t, I need to know. Flames welcome. ku ******** *@hotmail.com (Cocy)写道: 新闻:55 ************************* *@posting.google.c om:ku*********@hotmail.com (Cocy) wrote innews:55**************************@posting.google.c om:这可能是一种常见问题解答,但我不明白为什么,所以我有人会帮我理解什么是错的吗? 我刚刚创建了以下代码,用于修剪(给定)字符串中的空格。但是,它导致了分段错误,因此当在gdb中运行时(说程序接收信号 SIGSEGV,* p ++ = * st ++中的Segmentaion错误)。该平台是Linux内核2.4.27,gcc版本 2.95.4 20011002. / * ------------------- ------------------ * / #include< stdio.h> #include< ctype.h> int main(int argc,char ** argv) { char * st ="嘿,你好吗?" ;; Hi, This might be a sort of FAQ, but I don''t see why, so I would someone help me to understand what''s wrong? I''ve just created following code which wold trim white space(s) in a (given) string. But, it resulted the Segmentation fault, and so as when running in gdb (saying "Program received signal SIGSEGV, Segmentaion fault at *p++ = *st++"). The platform is Linux kernel 2.4.27, gcc version 2.95.4 20011002. /*-------------------------------------*/ #include <stdio.h> #include <ctype.h> int main(int argc, char ** argv) { char *st = "Hey, how are you?"; 如果你定义d这就是它的确如此: const char * st ="嘿,我无法修改!" ;; 原因应该跳出来(提示:注意''const''关键字)。 - - 马克 - > -If you defined this as it really is:const char *st = "Hey, I cannot be modified!";the reason should jump out at you (hint: note the ''const'' keyword).--- Mark ->-- Cocy写道:Cocy wrote:这可能是一种FAQ,但是我不明白为什么,所以我会有人帮我理解什么是错的? Hi, This might be a sort of FAQ, but I don''t see why, so I would someone help me to understand what''s wrong? http://www.eskimo.com/~scs/C-faq/q1.32.html Brian Rodenborn http://www.eskimo.com/~scs/C-faq/q1.32.htmlBrian Rodenborn 这篇关于复制角色时为什么分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 09:10