在全球范围内的指针的重新定义 [英] Redefinition of a pointer in global scope

查看:159
本文介绍了在全球范围内的指针的重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href=\"https://stackoverflow.com/questions/21151653/why-is-setting-a-derefernce-pointer-equal-to-a-primitive-illegal\">this问题我彻底的困惑C.这个看似基本的方面考虑code这两种界限:

In this question I am thoroughly confused about this seemingly basic aspect of C. Consider these two lines of code:

int *ptr;
*ptr = 2;

gcc会发出以下警告:

gcc will emit the following warnings:

main.cpp:4:1: warning: data definition has no type or storage class [enabled by default]

 *ptr = 2;
 ^
main.cpp:4:2: warning: type defaults to 'int' in declaration of 'ptr' [enabled by default]

 *ptr = 2;
  ^
main.cpp:4:8: warning: initialization makes pointer from integer without a cast [enabled by default]

 *ptr = 2;
        ^

PTR 被默认为, INT 为int * (如,为 PTR 指针,或 INT )?如果是这样,这是否意味着 PTR 指向地址2,或者是不变的?我会认为它的变化,因为它崩溃,除非你给 PTR 的有效地址。

What type is ptr being defaulted to, int or int* (as in, is ptr a pointer, or an int)? If so, does this mean that ptr is pointing to address 2, or is that unchanged? I would assume that it's changed because it crashes unless you give ptr a valid address.

int i = 5;
int *ptr;
*ptr = &i;

int main(){
    printf("%d", *ptr); // 5
}

我知道的未定义行为的可能性,并认为你不应该忽视的警告,但我想看看到底发生了什么在这里。

I am aware of the possibility of undefined behavior and that you shouldn't ignore warnings, but I'm trying to see what actually happens here.

有关的背景下,看到这个回答。

For context, see the comment chain under this answer.

推荐答案

下面是发生了什么事情:因为两行,你都出现在文件范围内(而不是局部范围)两条线被视为声明的,而不是作为一个声明和赋值语句。这是因为有可能是在文件范围内没有声明 - 只有声明和定义是允许

Here is what's going on: since the two lines that you are showing are in the file scope (as opposed to a local scope) both lines are treated as declarations, not as a declaration and an assignment statement. This is because there could be no statements at the file scope - only declarations and definitions are allowed.

旧的C规则允许的类型 INT 的声明会省略类型。因此,第二行是

Old C rules allowed declarations of type int to omit type altogether. Therefore, the second line is


  • 的声明/定义 PTR

  • ...这是一个指针,因为它有一个星号

  • ...,它也是一个指向 INT ,因为该类型缺失。

  • A declaration / definition of ptr
  • ...which is a pointer, because it has an asterisk
  • ...and it is also a pointer to int, because the type is missing.

这最后一条规则是非常陈旧,而且它已经在语言标准的ANSI版本pcated德$ P $。这就是为什么你会得到一个警告。如果您改变code到

That last rule is very archaic, and it has been deprecated in the ANSI version of the language standard. That is why you get a warning. If you change your code to

int *ptr;
int *ptr = &i;

您code是要编译和运行(演示)。

your code is going to compile and run (demo).

现在还有一个问题:怎么来编译器不抱怨重复的声明?事实证明,编译器将处理的多个相同的声明作为一个和相同的,只要它们是完全彼此相同。

Now one question remains: how come the compiler does not complain about duplicate declarations? It turns out that the compiler will treat multiple identical declarations as one and the same, as long as they are entirely identical to each other.

这篇关于在全球范围内的指针的重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆