Search code examples
objective-ccinitializationstructure

Odd use of curly braces in C


Sorry for the simple question but I'm on vacation reading a book on core audio, and don't have my C or Objective C books with me...

What are the curly braces doing in this variable definition?

MyRecorder recorder = {0};

Solution

  • Assuming that MyRecorder is a struct, this sets every member to their respective representation of zero (0 for integers, NULL for pointers etc.).

    Actually this also works on all other datatypes like int, double, pointers, arrays, nested structures, ..., everything you can imagine (thanks to pmg for pointing this out!)

    UPDATE: A quote extracted from the website linked above, citing the final draft of C99:

    [6.7.8.21] If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, [...] the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.