How to initialize a pointer to a struct in C?

You can do it like so:

static struct PipeShm * myPipe = &(struct PipeShm) {
    .init = 0,
    /* ... */
};

This feature is called a “compound literal” and it should work for you since you’re already using C99 designated initializers.


Regarding the storage of compound literals:

6.5.2.5-5

If the compound literal occurs outside the body of a function, the
object has static storage duration; otherwise, it has automatic
storage duration associated with the enclosing block.

Leave a Comment