Confusing error for incorrect struct initialization

lh mouse lh_mouse@126.com
Sat Jun 18 18:01:00 GMT 2016


From your point of view no C compiler could be 'good':
---
// gcc 6.1
test.c:4:20: error: initializer element is not constant
 struct foo bar = { s }; /*** should be &s ***/
                    ^
test.c:4:20: note: (near initialization for 'bar.p')
---
// clang 3.3
prog.c:4:20: error: initializing 'struct xxx *' with an expression of incompatible type 'struct xxx'; take the address with &
struct foo bar = { s }; /*** should be &s ***/
                   ^
                   &
prog.c:4:22: warning: missing field 'e' initializer [-Wmissing-field-initializers]
struct foo bar = { s }; /*** should be &s ***/
                     ^
---
// ICC 13.0
example.cpp(4): error: expression must have a constant value
struct foo bar = { s }; /*** should be &s ***/
---
// CL18  (a.k.a. MSVC)
test.c
test.c(4) : error C2099: initializer is not a constant
---

The error suppresses the warning here.
The standard is the standard. The WG14 people don't care what you would think about them and the language. 

If you truly want to have some error messsages more friendly (thus helpful), particularly for your sample code above, you can be happy with C++.


------------------				 
Best regards,
lh_mouse
2016-06-19

-------------------------------------------------------------
发件人:Mason <mpeg.blue@free.fr>
发送日期:2016-06-19 01:42
收件人:lh mouse
抄送:GCC help
主题:Re: Confusing error for incorrect struct initialization

On 18/06/2016 18:42, lh mouse wrote:

> Initializing a `int *` with an `int` has to be permitted.
> It is making it an error that would be a bug.

The code I'm interested in is:

struct xxx { void *p; int a,b,c,d; double x,y; };
struct foo { struct xxx *p; int e,f,g; };
struct xxx s;
struct foo bar = { s }; /*** should be &s ***/

(I don't see any int pointers there.)

Do you see the error on the last line?
What error message would you expect a good compiler to output?

Regards.





More information about the Gcc-help mailing list