【发布时间】:2017-09-10 03:16:00
【问题描述】:
我有一个指向字符数组的字符指针。 我想清除字符数组,然后在其中复制一些其他数组。memset 不适用于 char 指针。 有没有其他方法可以做到这一点?
int main(){
char a[100] = "coepismycollege";
char *p;
p = a;
test(p);
printf("After function : %s", a);
}
void test(char *text){
char res[120] = "stackisjustgreat";
printf("text = %s\nres = %s\n", text , res);
memset(&text, 0 , sizeof(*text));
strcpy(text, res);
}
输出应该是:stackisjustgreat
提前致谢。
【问题讨论】:
-
memset(&text, 0 , sizeof(*text));-->memset(text, 0 , strlen(text));但在这种情况下memset是不必要的。 -
也没用!
-
@Savan BLUEPIXY 的评论是正确的。如果它对您不起作用,那么您需要发布minimal, complete, and verifiable example,因为您的代码无法编译。要获取有关代码问题的更多信息,请在 gcc 或 clang 上使用
-Wall进行编译。 -
你是对的@BLUEPIXY
标签: c arrays pointers character