char *s和char s[]的问题。

falcon1210 2005-06-05 08:26:24
#include <stdio.h>
#include <string.h>
int main()
{
char *s = "fdsf fdsf fds";

//char s[] = "fdsf fdsf fds";

const char *d = " ";
char *p;

p = strtok(s,d);

if(p)
printf("%s\n",p);

return 0;
}

为什么这样不可以,用char s[]就可以。
附上strtok

(2005-06-05 20:03:35) 天涯-晴空
函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>

int main(void)
{
char input[16] = "abc,d";
char *p;

/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s\n", p);

/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}

...全文
195 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
CPPLOVER_78 2005-06-06
  • 打赏
  • 举报
回复
同意 看《高质量c++编程》,里面可以系统的解决你的问题。
//是本书基本都会说。
cppprogramlover 2005-06-05
  • 打赏
  • 举报
回复
看《高质量c++编程》,里面可以系统的解决你的问题。
//是本书基本都会说。
hundlom 2005-06-05
  • 打赏
  • 举报
回复
看《高质量c++编程》,里面可以系统的解决你的问题。
csjcrystal 2005-06-05
  • 打赏
  • 举报
回复
由于char*是在常量区的,因此那部分是不能操作的,也不能改变
而char[]是分配在堆栈的
falcon1210 2005-06-05
  • 打赏
  • 举报
回复
楼上能不能详解下,谢谢啦
truewill 2005-06-05
  • 打赏
  • 举报
回复
很old的问题了

因为char*没有分配缓冲区,只是一个常量区

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧