C中整数指针数组的静态初始化出错

时间:2022-09-18 21:17:51

I am getting an error in initialization of array of integer pointer. Though the issue seems simple I am unable to understand what's wrong in this. I am new in C programming.

我在初始化整数指针数组时遇到错误。虽然这个问题看似简单,但我无法理解这有什么不妥。我是C编程的新手。

C中整数指针数组的静态初始化出错

2 个解决方案

#1


5  

The C90 standard said (in §6.5.7):

C90标准说(§6.5.7):

All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

具有静态存储持续时间的对象的初始值设定项中的所有表达式或具有聚合或联合类型的对象的初始化列表中的所有表达式都应为常量表达式。

In context, p 'has aggregate type', and the addresses of the array elements of a are not constants, so C90 says that initialization is not allowed. That's why your compiler rejects that code.

在上下文中,p'具有聚合类型',并且a的数组元素的地址不是常量,因此C90表示不允许初始化。这就是你的编译器拒绝该代码的原因。

C99 relaxes that restriction. The corresponding paragraph (§6.7.8 ¶4) says:

C99放松了这种限制。相应的段落(§6.7.8¶4)说:

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

具有静态存储持续时间的对象的初始值设定项中的所有表达式应为常量表达式或字符串文字。

Do yourself a favour and get a compiler that dates from the current millennium — it should be one that implements C11 if at all possible.

帮自己一个忙,并获得一个可以追溯到当前千年的编译器 - 如果可能的话应该是一个实现C11的编译器。

#2


3  

Believe it or not but the problem is actually your super ancient compiler. The code is fine. Just use a new compiler. Today most of the compilers are free and very small in size for downloading. So consider to upgrade to gcc or MSVC.

信不信由你,但问题实际上是你的超级古老的编译器。代码很好。只需使用新的编译器。今天大多数编译器都是免费的,并且非常小,可以下载。所以考虑升级到gcc或MSVC。

#1


5  

The C90 standard said (in §6.5.7):

C90标准说(§6.5.7):

All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

具有静态存储持续时间的对象的初始值设定项中的所有表达式或具有聚合或联合类型的对象的初始化列表中的所有表达式都应为常量表达式。

In context, p 'has aggregate type', and the addresses of the array elements of a are not constants, so C90 says that initialization is not allowed. That's why your compiler rejects that code.

在上下文中,p'具有聚合类型',并且a的数组元素的地址不是常量,因此C90表示不允许初始化。这就是你的编译器拒绝该代码的原因。

C99 relaxes that restriction. The corresponding paragraph (§6.7.8 ¶4) says:

C99放松了这种限制。相应的段落(§6.7.8¶4)说:

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

具有静态存储持续时间的对象的初始值设定项中的所有表达式应为常量表达式或字符串文字。

Do yourself a favour and get a compiler that dates from the current millennium — it should be one that implements C11 if at all possible.

帮自己一个忙,并获得一个可以追溯到当前千年的编译器 - 如果可能的话应该是一个实现C11的编译器。

#2


3  

Believe it or not but the problem is actually your super ancient compiler. The code is fine. Just use a new compiler. Today most of the compilers are free and very small in size for downloading. So consider to upgrade to gcc or MSVC.

信不信由你,但问题实际上是你的超级古老的编译器。代码很好。只需使用新的编译器。今天大多数编译器都是免费的,并且非常小,可以下载。所以考虑升级到gcc或MSVC。