使用puts()在指针指向的位置覆盖新值

时间:2018-11-16 10:41:55

标签: c

我希望得到一些建议。我非常感谢你。

char *p = "abc";
printf("Value at the location the pointer points to = %s\n", p);
printf("The location where the pointer points to = %d\n", p);
printf("The location of the pointer = %d\n", &p);
char s[] = "Hello";
printf("Vi tri cua s = %d\n", &s);
gets(s);  //we must enter less than 5 characters, because we have s[5]
puts(s);
printf("The new value at the location the pointer points to:\n");
gets(p);  //This function cause problem, while the function gets(s) does not
puts(p);

1 个答案:

答案 0 :(得分:0)

"abc"是文字,并且受写保护(只读)。因此,您无法阅读它。

char s[] = "Hello";创建一个数组并将文字字符串复制到其中。结果数组不是文字,因此可以修改。