簡體   English   中英

struct成員“ Initializer元素不是編譯時常量”

[英]struct member 'Initializer element is not a compile-time constant'

struct S {
   int a;
};

int a = ((struct S) {8}).a;

編譯器報告錯誤“ Initializer元素不是編譯時常量”,為什么?

因為括號中的那個結構實際上是運行時的東西。 您只能在初始化時將常量分配給全局變量。 例如

 int a = 8;

您不能使用全局變量執行此操作:

int b = 8;
int a = b; // Because b is a run-time variable

通常,此技術用於定義全局常量:

#define MY_CONSTANT 8
// Then somewhere else you can use it...
int a = MY_CONSTANT;
// or
struct S s = {MY_CONSTANT};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM