小编Chr*_*Liu的帖子

为什么全局指针不能重新指向另一个对象,而局部指针可以?

我是 C 的新手,尤其是 C 指针的新手。这里的问题很简单:

#include <stdio.h>

char *p1 = "Hello";
char *p2;
p2 = "world";  // error: redefinition of 'p2' with a different type: 'int' vs 'char *'

int main()
{
   char *p3 = "Hello";
   char *p4;
   p4 = "world";  // OK!  p4 can re-point to somewhere
   
   return  0;
}
Run Code Online (Sandbox Code Playgroud)

我声明了一个全局指针p2,但我尝试为其分配一个字符串"hello"? 。
但是QT提示一个错误: redefinition of 'p2' with a different type: 'int' vs 'char *'

它说p2是一个int?但我清楚地宣布它是char *;

但是,p4 …

c pointers

3
推荐指数
1
解决办法
55
查看次数

标签 统计

c ×1

pointers ×1