Preliminary note: member name
of your structure is an array, not a pointer. NULL
is not a valid value for it. The value it would be initialized with by default initialization is all(30)-bytes-zero, which is an entirely different thing.
With that said, you get default initialization by, um, default for objects declared with static storage duration. That is, all those declared at file scope and those declarared at block scope with the keyword static
. For those cases you don't need to do anything special.
// at file scope
struct employee emp1; // done
int do_something() {
static struct employee emp2; // done (but any changes persist across calls)
// ...
return 0;
}
For those with automatic storage duration -- that is, block-scope variables that are not static -- you need to provide an initializer. It is important here to understand that
- although braces are required around the initializer for an aggregate, nested braces are optional for the initializers of nested aggregates;
- the constant
0
can be used to initialize any scalar, and the result is the same as what default initialization would achieve; and
- if an initializer for an aggregate does not initialize all members then those not explicitly initialized are implicitly default initialized.
What that all means is that you can achieve manual initialization equivalent to default initialization for any object via the initializer {0}
(though be careful here with arrays of unspecified length). In particular, this is utterly idiomatic:
int do_something_else() {
struct employee emp3 = {0};
// ...
return 0;
}
Note well that initialization is not assignment! That syntax is not (quite) valid for an assignment statement.
And that brings us around to the last case: allocated objects. These are not subject to default initialization and they cannot have initializers. It is fairly common to set initial values for these by setting them to all bytes zero, either by allocating them with calloc()
or by zero-filling them after allocation with memset()
. Neither of those approaches supports strictly conforming programs in all cases, however, because pointer and floating-point object representations with all bits zero are not necessarily the same as you get from default initialization of such objects.
You can always just assign appropriate values to each member individually, but in modern C, the cleanest thing to do to set an allocated structure's value as if it had been default initialized is to assign an appropriate a compound literal value to it:
int do_a_third_thing() {
struct employee *ep = malloc(sizeof(*ep));
// ... check for / handle NULL ...
*ep = (struct employee) {0}; // HERE
// ...
return 0;
}
In that example, the expression (struct employee) {0}
has type struct employee
and (initial) value as if it were declared with an initializer of {0}
.
Of course, you can do the same thing to assign a new, as-if-default-initialized value to any structure or union object, regardless of its storage duration.
With all that said, the bottom line answer to
I want all employees (objects) created from that structure to have ID
= 0, name = NULL, and salary = 0. Is this possible?
Is no, it is not possible per se. C does not have constructors, default or otherwise. As I have shown, however, if default initialization is what a user wants for a particular instance, then it is fairly easy for them to get it. And if it's not what they want, then it is not useful to force that on them.