Namespaces
Variants
Actions

Talk:c/language/struct initialization

From cppreference.com

Failed to build with ERROR message: expected primary-expression before ‘.’ token

I'm using gcc 11.2.0 under Ubuntu 22.04.

It looks this version of gcc 11.2.0 won't be able to compile that particular ., which is from a .cpp file, rather than .c file.

Can anybody please help to build the following code through ??


#include <stdio.h>
typedef struct { int k; int l; int a[2]; } T;
typedef struct { int i; T t; } S;
T x = {.l = 43, .k = 42, .a[1] = 19, .a[0] = 18 };
 // x initialized to {42, 43, {18, 19} }
int main(void)
{
 S l = { 1, // initializes l.i to 1
 .t = x, // initializes l.t to {42, 43, {18, 19} }
 .t.l = 41, // changes l.t to {42, 41, {18, 19} }
 .t.a[1] = 17 // changes l.t to {42, 41, {18, 17} }
 };
 printf ("l.t.k is %d\n", l.t.k); // .t = x sets l.t.k to 42 explicitly
 // .t.l = 41 would zero out l.t.k implicitly
}

-- that's a C program, not a C++ program. GCC will attempt to use C++ by default if you give it a .cpp file. unless you override with -x c --Cubbi (talk) 10:04, 1 August 2022 (PDT)

-- Thank you for the prompt reply. I understand now. However, refer to [1]. I was actually trying to build Filament, [2], this PostProcessManager.cpp does come with the struct/union initialization in C-style. Filament's google developer said: they can build everything successfully with CLANG. Anyway, is there a way to build this C-Style struct/union initialization with C++ compiler?

I also found [3]

Jiapei100 (talk) 10:11, 1 August 2022 (PDT)

right, the "See also" link at the bottom of this page leads to the C++ page that describes the rules of the similar feature in that language (specifically, in cpp/language/aggregate_initialization#Designated_initializers). Several compilers supported some variants of that as a language extension for many years until they finally agreed on what exactly they are supporting, in C++20 --Cubbi (talk) 11:54, 1 August 2022 (PDT)
Retrieved from "https://en.cppreference.com/mwiki/index.php?title=Talk:c/language/struct_initialization&oldid=141642"

AltStyle によって変換されたページ (->オリジナル) /