Reputation: 1774
Does the following zero out a list and then add the first entry? Or does it just add the first entry to handlers
?
Logger = (struct logger) {.level=INFO, .format=DEFAULT_FORMAT, .num_handlers=1, .handlers[0]=stdout};
For example, does this do:
Logger.handlers = {0};
Logger.handlers[0] = stdout;
Or is there no clearing involved?
Upvotes: 0
Views: 56
Reputation: 84561
For example, does this do:
Logger.handlers = {0}; Logger.handlers[0] = stdout;
Answer: Yes (but not specifically in that order)
There are three standard sections (actually 4 if you take the section that specifically makes the following two applicable to "aggregate objects" (struct and arrays)) under the section C11 Standard § 6.7.9 - Initialization. Your case asks "What happens to the other elements of Logger.handlers
if only Logger.handlers[0] = stdout;
is provided a value during initialization?"
To answer that question, you need to look at § 6.7.9 Initialization (p19) and § 6.7.9 Initialization (p21)
The first specifies how initialization of structs occurs in "list order" and that "all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration."
So if you provide fewer than all values to fully initialize a subobject, those not provided a value are initialized as if they have static storage duration. (objects with static storage duration, not explicitly initialized, are initialized to zero (or NULL
, as appropriate for type), See: § 6.7.9 Initialization (p10))
Paragraph 21 specifically covers your case where fewer initializers are provided within a brace enclosed list -- "all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration."
§ 6.7.9 Initialization (p21)
So putting it altogether, what actually happens for the initialization of Logger.handlers
is the first element is initialized to stdout
and that all other elements are initialized NULL (since stdout
is a pointer of type FILE*
). So what you would actually have is:
Logger.handlers[CONST] = { stdout, NULL, NULL, ... };
Look things over (read all of § 6.7.9), and let me know if you have further questions.
Upvotes: 1
Reputation:
The latter Logger.handlers[0] = stdout;
. This is why the API has a .num_handlers field to tell called code know how many of the handlers are valid.
Upvotes: 0