|
|
我在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? |
|