因为在全局作用域中将变量的地址赋给指针导致警告错误。

huangapple go评论52阅读模式
英文:

why am i getting warnings errors due to assignment of a variable's address to a pointer in the global scope?

问题

以下是要翻译的内容:

这是我在尝试编译程序时收到的警告截图。我刚刚开始学习指针,但编译器对以下程序进行了标记,原因我无法理解。代码如下:

#include <stdio.h>

int dec = 0;
int *d;
d = &dec;

int main() {
    return 0;
}

当我将这些声明放入main函数的主体时,就没有错误。我使用的gcc版本是gcc 12.2.0(通过MSYS2下载),代码编辑器是MS Visual Code。是否有人能够提供解释?

正如我上面所述,我随机开始编写一个程序,以熟悉指针,我预期无论指针在何处声明和初始化,它们的处理都不会有任何变化。

英文:

this the snip of the warning i was getting when i tried to compile the program.I am just getting started with pointers and this following program is being flagged by compiler for some reason I am not able comprehend. the code is as follows:

#include &lt;stdio.h&gt;

int dec = 0;
int *d;
d = &amp;dec;

int main() {
    return 0;
}

there is no error when I am stuffing these declarations in to main's body. the version of gcc I am using is gcc version 12.2.0(downloaded using MSYS2) and code editor MS visual code.can anybody post an explanation for this?

as i have stated above i have randomly started typing a program to get familiar with pointers, i expected there to be no variation in the treatment of pointers regardless of where they are being declared and intialised.

答案1

得分: 2

你试图在函数外部执行赋值操作,这是不允许的。你可以进行初始化操作:

int *d = &dec;
英文:

You're attempting to perform an assignment outside of a function, which is not allowed. What you can do is initialize:

int *d = &amp;dec;

答案2

得分: 1

在文件作用域中,您只能使用声明。

在提供的程序中,您在文件作用域中使用了一个赋值语句

d = &amp;dec;

因此,编译器会报错。

相反,您可以这样写,例如:

#include &lt;stdio.h&gt;

int dec = 0;
int *d = &amp;dec;

int main( void ) {
    return 0;
}

由于变量 dec 具有静态存储期,因此表达式 &amp;dec 是一个地址常量,可以用作变量 d 的初始化器,该变量也具有静态存储期。

根据C标准(6.7.9 初始化)

> 4 对于具有静态或线程存储期的对象的初始化器中的所有表达式都必须是常量表达式或字符串文字

以及(6.6 常量表达式)

> 7 初始化器中的常量表达式允许更多的自由度。这样的常量表达式必须是以下之一,或者评估为以下之一:

> — 算术常量表达式,

> — 空指针常量,

> — 地址常量,或

> — 完整对象类型的地址常量加或减整数常量表达式。

> 9 地址常量是空指针,指向具有静态存储期的对象的lvalue的指针,或者指向函数标识符的指针;它必须使用一元&操作符显式创建,或者通过使用数组或函数类型的表达式隐式创建。数组下标 [] 和成员访问 . 和 -> 操作符,地址 & 和间接 * 一元操作符以及指针转换都可以用于创建地址常量,但不得使用这些操作符来访问对象的值。

英文:

You may use only declarations in file scopes.

In the provided program you are using an assignment statement

d = &amp;dec;

in the file scope. So the compiler issues an error.

Instead you could write for example

#include &lt;stdio.h&gt;

int dec = 0;
int *d = &amp;dec;

int main( void ) {
    return 0;
}

As the variable dec has static storage duration then the expression &amp;dec is an address constant and my be used as an initializer for the variable d that also has static storage duration.

From the C Standard (6.7.9 Initialization)

> 4 All the expressions in an initializer for an object that has static
> or thread storage duration shall be constant expressions or string
> literals
.

and (6.6 Constant expressions)

> 7 More latitude is permitted for constant expressions in initializers.
> Such a constant expression shall be, or evaluate to, one of the
> following:
>
> — an arithmetic constant expression,
>
> — a null pointer constant,
>
> — an address constant, or
>
> — an address constant for a complete object type plus or minus an
> integer constant expression.

and

> 9 An address constant is a null pointer, a pointer to an lvalue
> designating an object of static storage duration
, or a pointer to a
> function designator; it shall be created explicitly using the unary
> & operator
or an integer constant cast to pointer type, or
> implicitly by the use of an expression of array or function type. The
> array-subscript [] and member-access . and -> operators, the address &
> and indirection * unary operators, and pointer casts may be used in
> the creation of an address constant, but the value of an object shall
> not be accessed by use of these operators.

huangapple
  • 本文由 发表于 2023年1月9日 02:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定