本文介绍了哪里错了?怎么纠正?为什么?谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 void main() { char * p =" Hello"; printf("%s",p ); * p =''w''; printf("%s",p); } 解决方案 作业* p =' 'w''修改字符串文字,导致未定义的 行为。在大多数实现中,字符串文字驻留在只读 内存中。因此* p =''w''会导致段错或类似的事情。 请改为:char p [6] =" Hello"; - / - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰------ - $ \\ \ - http:// www .helsinki.fi / ~palaste ---------------------规则! -------- / 这是一个人员通勤者。 - 科学美国人的火车司机 更正: #include< stdio.h> int main(无效) { char p [] =" Hello"; printf("%s \ n",p); p [0] = ''w''; printf("%s \ n",p); 返回0; } - Andreas K?h?ri void main(){char* p="Hello";printf("%s",p);*p=''w'';printf("%s",p);} 解决方案 The assignment *p=''w'' modifies a string literal, causing undefinedbehaviour. On most implementations, string literals reside in read-onlymemory. Thus *p=''w'' can cause a segfault or something similar.Try instead: char p[6]="Hello"; --/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/"This is a personnel commuter."- Train driver in Scientific American Corrected: #include <stdio.h> int main(void){char p[] = "Hello";printf("%s\n", p);p[0] = ''w'';printf("%s\n", p);return 0;}--Andreas K?h?ri 这篇关于哪里错了?怎么纠正?为什么?谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 08:12