hitchhiker
hitchhiker

Reputation: 1319

Is there any difference between initializing a struct with {0} or {'\0'}?

A work colleague recommended to initialize a struct with {'\0'} instead of {0} because he explained that0 is considered to be an int thus at least 4 bytes. Therefore if a struct is not a multiple of 4 initializing the struct to 0 may leave some bytes not initialized to 0 while because \0 is an ASCII char of size 1 byte it will initialize a struct to zero regardless of its size.

That being said I almost never saw initialization using {'\0'} while I did see lots of initializations using {0}.

My question is whether initializing a struct with {0} is enough? Is it enough because most compilers will automatically pad the bytes which are not multiple of 4 with zeroes or is it because C does it?

Upvotes: 3

Views: 347

Answers (2)

melpomene
melpomene

Reputation: 85767

No, there is no difference.

For one, '\0' is not "one byte"; in C, character constants have type int, so '\0' and 0 are 100% equivalent in every context. (You can check this with sizeof: Both sizeof 0 and sizeof '\0' will yield the same value, typically 4 or 8.)

The other reason is explained in the initialization rules of C (in C99 that's 6.7.8 Initialization):

[...]

  1. Otherwise, the initializer for an object that has aggregate or union type shall be a brace-enclosed list of initializers for the elements or named members.

  2. Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union. [...]

This says the members of the initialization list are used to initialize the fields of a struct or array in order; it doesn't matter how many bytes they have.

If you write

struct foo { double x; };
struct foo var = { 0 };

then 0 (the first initializer value, type int) is used to initialize the first struct field (x, type double). It's as if you had written double x = 0. The effect is that the value of 0 is implicitly converted from int to double and stored in the variable.

Furthermore, if there are fewer initializers than struct or array elements, this rule kicks in:

  1. If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

So how does implicit initialization work with static objects?

  1. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

    • if it has pointer type, it is initialized to a null pointer;
    • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    • if it is an aggregate, every member is initialized (recursively) according to these rules;
    • if it is a union, the first named member is initialized (recursively) according to these rules.

This means all not-explicitly-initialized members of an array or struct are implicitly set to 0.

Upvotes: 10

Hasit Bhatt
Hasit Bhatt

Reputation: 346

Yes, initializing a struct with {0} is enough. Understand the compiler doesn't assign the memory to struct based on the initialization. A struct independent of whether it's filled with some value or not will always require the same memory.

e.g.

#include <stdio.h>

struct A {
    char a;
};

struct B {
    int b;
};

int main(void) {
    struct A b = {'\0'};
    struct A c = {0};
    printf("%zu %zu\n",sizeof(b),sizeof(c));

    struct B ab = {'\0'};
    struct B ac = {0};
    printf("%zu %zu\n",sizeof(ab),sizeof(ac));

    return 0;
}

The answers will always be the same independent of how you assign it.

Upvotes: 0

Related Questions