strtok_看看这个程序的问题,为什么不能赋值呢

[复制链接]
查看11 | 回复2 | 2009-4-30 19:56:31 | 显示全部楼层 |阅读模式
#include
#include
#include
int main(){
char st[100];//输入的字符串
char a[5][10];//将字符串按空格分割成多个字符串存放在a中
while(1){
scanf("%s",st);
char *p;
p=strtok(st," ");
strcpy(a[0],p);
int i=0;
while(p!=NULL){
puts(p);
p=strtok(NULL," ");
strcpy(a[i++],p);————这句会错阿,为什么?
}
}return 0;
}

回复

使用道具 举报

千问 | 2009-4-30 19:56:31 | 显示全部楼层
因为strtok返回NULL时候,strcpy(a[i++,NULL);就出错了加个保护#include #include //#include int main(){char st[100];//输入的字符串char a[5][10];//将字符串按空格分割成多个字符串存放在a中while(1) { int i=0; char *p; scanf("%s",st);
p=strtok(st," "); strcpy(a[0],p);
while(p!=NULL)...
回复

使用道具 举报

千问 | 2009-4-30 19:56:31 | 显示全部楼层
scanf("%s",st);-- 读到空白就不读了。要用 gets 读入全行,含空白。...
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行