【发布时间】:2019-02-28 19:56:35
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *str, *temp;
str = malloc(sizeof(char) * 100);
fgets(str, 100, stdin);
temp = str;
printf("%s", str);
return 0;
}
这是在不使用strcpy() 函数的情况下将一个字符串复制到另一个字符串的有效代码吗?
【问题讨论】:
-
它没有复制字符串。它正在分配指针,它本身不是无效的,但有特定的用例。
-
你可以自己检查
str[0] = '*';,然后printf("%s", temp);,看看你也修改了temp。
标签: c string pointers dereference