【发布时间】:2014-06-06 05:04:07
【问题描述】:
我需要将 tmp 的内容保存到 tmp2。但是,在 while 循环之外,tmp 始终为 NULL。
if(1){
char* tmp;
char* tmp2;
// split the string on the space character
tmp = strtok(actual_args[0], " ");
while(tmp != NULL){
strcpy(tmp2, tmp);
tmp = strtok(NULL, " ");
}
// always NULL
printf("%s", tmp);
// produces seg. fault
printf("%s", tmp2);
}
【问题讨论】:
-
当然
tmp在while 之外是NULL,因为这正是终止循环的条件。而且你不能strcpy指向一个不指向任何地方的指针。您需要一些存储空间供tmp2指向。如果您告诉我们您要做什么,我们可以向您展示正确的方法。 -
在每次迭代期间不会将 tmp 的值写入 tmp2,以 tmp 的最后一个值结束(在它变为 null 之前)吗?
-
在你的代码中没有任何东西被正确地写入
tmp2。你想做什么? -
我正在尝试获取 actual_args[0] 中的最后一个令牌。