Reputation: 27435
The C18 Standard states at 6.7.9/2
that:
No initializer shall attempt to provide a value for an object not contained within the entity being initialized.
It is not really clear what it means. There is a related topic: Incosistent gcc diagnostic for string initialization. The clause I cited was used to explain the error produced by the following initialization:
//error: excess elements in array initializer char a[5]
char a[5] = {'h','e','l','l','o','\0'};
Where the initializer-list
has length exceeding the size of the array being initialized.
But consider more trivial example:
int main(void){
int a;
int b = (a = 3);
}
The initializer here is (a = 3)
which is an assignment-expression
. And the initializer assigns a value to another object which should cause constraint-violation.
Why isn't any diagnostic printed?
Upvotes: 4
Views: 1006
Reputation: 215507
That's not "providing a value for a
". It's an expression with a side effect of storing a value in a
. "Providing a value" should be understood in the context of the initialization; it's not phrasing used for assignments.
A less trivial example of what the language in the standard is trying to get at might be:
struct {
char a[4];
char b[4];
} foo = { { 'h', 'e', 'l', 'l', 'o' } };
whereas (I think; correct me if I'm wrong) the following would be valid, albeit confusing, C:
struct {
char a[4];
char b[4];
} foo = { 'h', 'e', 'l', 'l', 'o' };
Upvotes: 5