免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 5426 | 回复: 28
打印 上一主题 下一主题

[函数] 函数调用错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-02-17 11:24 |只看该作者 |倒序浏览
  1.         char *s1,*s2,*p2;
  2.         int n;
  3.         s1 = "this is a test";
  4.         s2 = "asda";
  5.         n = strxfrm(s1,s2,3);//出问题
  6.         printf("%d\n",n);
  7.         p2 = strtok("The summer soldier,the sunshine patriot"," ");//出问题
复制代码


我标记的2处都是出现同样的问题:Segmentation Fault,弄半天没有搞明白?为什么函数调用就出现问题了?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-02-17 11:28 |只看该作者

函数调用错误

这两个函数不能用常量字符串
也就是说s1、s2指向的是常量,不能做修改

论坛徽章:
0
3 [报告]
发表于 2005-02-17 13:01 |只看该作者

函数调用错误

如果可执行文件是 ELF 格式,那么 s1, 和 s2 的内存将分配在代码段中,该页面是只读的。

论坛徽章:
0
4 [报告]
发表于 2005-02-17 13:04 |只看该作者

函数调用错误

那char s1[] = "this is a test";和char *s1 = "this is a test";有什么区别呢?为什么在以上二个函数中一个可以被修改,另一个却不可以?

论坛徽章:
0
5 [报告]
发表于 2005-02-17 13:06 |只看该作者

函数调用错误

原帖由 "wqch" 发表:

我标记的2处都是出现同样的问题:Segmentation Fault,弄半天没有搞明白?为什么函数调用就出现问题了?

兄弟这个不是C吧?

论坛徽章:
0
6 [报告]
发表于 2005-02-17 13:18 |只看该作者

函数调用错误

to liulang0808 :
你说哪儿不是C啊?这个可是ANSI C哦!
to escapedog :
局部变量为什么跑到代码段了,难道ELF有新规定了?!

论坛徽章:
0
7 [报告]
发表于 2005-02-17 13:24 |只看该作者

函数调用错误

原帖由 "wqch" 发表:
to liulang0808 :
你说哪儿不是C啊?这个可是ANSI C哦!
to escapedog :
局部变量为什么跑到代码段了,难道ELF有新规定了?!

我刚学C语言,

s1 = "this is a test";
s2 = "asda";


我看的C语言是不可以这样定义字符串的。
只能字符数组!

论坛徽章:
0
8 [报告]
发表于 2005-02-17 14:13 |只看该作者

函数调用错误

对不起,没说清楚是 "this is a test"  和 "asda" 两个字符串分配在代码段。

论坛徽章:
0
9 [报告]
发表于 2005-02-17 14:19 |只看该作者

函数调用错误

[quote]原帖由 "wqch"]s is a test";和char *s1 = "this is a test";有什么区别呢?为什么在以上二个函数中一个可以被修改,另一个却不可以?[/quote 发表:


因为第一种写法,编译器在栈上为数组 s1 分配内存并复制字符串到数组中去,第二种写法编译器为指针 s1 分配内存,并指向代码段储存字符串的地址。这两种写法是完全不同的,国内的 C 语言书大部分书上说两种写法相同是错误的。误人子弟!

论坛徽章:
0
10 [报告]
发表于 2005-02-17 14:45 |只看该作者

函数调用错误


There is an important difference between these definitions:

   char amessage[] = "now is the time"; /* an array */
   char *pmessage = "now is the time"; /* a pointer */

amessage is an array, just big enough to hold the sequence of characters and '\0' that initializes it. Individual characters within the array may be changed but amessage will always refer to the same storage. On the other hand, pmessage is a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents.


--《the TCPL》K&R

真是啊,刚从里面找出来!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP