全局区初始化char指针为NULL后赋值字符串报错的问题咨询
全局区初始化char指针为NULL后赋值字符串报错的问题咨询
我最近碰到一个C语言的问题,琢磨了好久都没搞明白,想请教下大家:
当我尝试把一个char指针在全局区初始化为NULL,之后再给它赋值字符串的时候,编译会抛出错误,具体报错信息如下:
error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
Ptr = "NULL";
^
int
error: redefinition of 'Ptr' with a different type: 'int' vs 'char *'
note: previous definition is herechar *Ptr = NULL;
我写了这段测试代码,只要是在全局区先把指针初始化为NULL再赋值字符串,就一定会报错:
char *Ptr = NULL; Ptr = "NULL"; int main() { return 0; }
但如果直接把指针初始化为字符串就完全没问题;另外要是把这段变量定义和赋值的逻辑放到main函数里(作为局部变量),也能正常编译运行:
int main() { char *Ptr = NULL; Ptr = "NULL"; return 0; }
有没有大佬能帮我理清这里面的底层逻辑呀?
备注:内容来源于stack exchange,提问作者M_G_0xFF
相关产品推荐
相关产品推荐

