Why did coder assign value of struct pointer to a static struct?

Apologies if this is a duplicate, in which case I couldn't find the right keywords to search.

This is in reference to some old (MUD) code I'm working on which is pasted below. I'm confused by purpose of the foo_zero and *foo = foo_zero parts of the code below. This is a pattern it uses throughout the codebase. My guess is that it's a way of initializing all of the members of foo to zero/NULL without having to explicitly set them.

typedef struct FOO {
    int buzz;
    char *bazz;
};

FOO *init_foo(void)
{
    static FOO foo_zero;
    FOO *foo;
    
    foo = malloc(sizeof(*foo));
    *foo = foo_zero; // <-- why?
    return foo;
}


from Recent Questions - Stack Overflow https://ift.tt/35RTsMf
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)