LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 662|回复: 3

Where is the bug????????????

[复制链接]
发表于 2005-8-18 10:09:35 | 显示全部楼层 |阅读模式
我在linux库函数中看到的例题:
#include<string.h>
main()
{
char s[]=”ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z”;
char *delim=”-: “;
char *p;
printf(%s “;strtok(s,delim));
while((p=strtok(NULL,delim)))
       printf(“%s   ”,p);
printf(“\n”);
}
在gcc下编译通过,运行时也可以得到正确的结果.
可是当我把 char s[] 改为 char *s ,其他都不变.在gcc下能通过编译,但是运行时得到的结果提示说Segmentation fault.
我在gdb中试过,错误好像是发生在动态连接库中..
按我的理解,下面的两条语句应该是等价的呀.
char s[]=”ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z”;
char *s=”ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z”;

Where is the bug??
why?
发表于 2005-8-18 10:28:39 | 显示全部楼层
前面的帖子里有同样的问题,回答众多。
http://www.linuxsir.cn/bbs/showthread.php?t=208406
回复 支持 反对

使用道具 举报

发表于 2005-8-18 10:32:03 | 显示全部楼层
char s[]=”ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z”;  //定义一个数组,数组里的值可更改
char *s=”ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z”;  //定义一个指针,指向的字串不可更改

strtok函数要更改所指字串的值。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-8-18 13:17:24 | 显示全部楼层

多谢啦................

多谢啦................
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表