1. 16
  1.  

  2. 4

    The article says:

    it also gives C99 a portable way of using optional and keyword arguments

    but doesn’t elaborate on it. I assume he means this: https://github.com/mcinglis/c-style#use-structs-to-name-functions-optional-arguments

    edit: on second thoughts he probably doesn’t but I’ll leave it up because I think it’s cool

    1. 3

      Yes, it’s along similar lines as the “Always use designated initializers in struct literals” section in that document. If you refer to struct fields with the .field_name syntax, then any fields you don’t mention are initialized to 0. This means that you can check for zeroed fields inside the function and replace them with default arguments. Also, since the fields are referred to by name, there’s a bit more flexibility than if the fields' order was part of the API.

      My socket99 library takes a pointer to a config struct. The document you linked to is wrapping the function in a __VA_ARGS__ macro, which does that behind the scenes, but doesn’t seem to acknowledge that the macro is unnecessary.

    2. 1

      On a side note, I use the “object initializer” style in C# quite often which seems to be similar to what the article is describing for c99:

      var p = new Person() {FirstName=“John”,LastName=“Doe”,Phone=“602-123-1234”,City=“Phoenix”};

      The downside, is you can easily forget a required setting, and the compiler won’t help you find missing initializers. You can, as mentioned by @SilentBicycle, simply check for zeros in required fields when the structs are used.

      A regular constructor with parameters doesn’t suffer from this.