Search code examples
ccurly-bracescompound-literals

What curly braces inside a function call mean in C?


I tried to solve exercise 1-24 in K&R C book in which you have to create a program which can detect basic syntax errors (unbalanced parentheses, brackets and so on). I ran some tests to debug it on C source files scattered on my system. My program detected an error when it met this line in a file :

av_opt_set_q  (abuffer_ctx, "time_base", (AVRational ){ 1, INPUT_SAMPLERATE }, AV_OPT_SEARCH_CHILDREN);

I made the assumption that, every time a regular curly bracket is encountered (outside comments, double quotes), parentheses and brackets must be balanced. This is not true as this error showed. Unfortunately I cannot find what it means. Thanks for your help.


Solution

  • This

     (AVRational ){ 1, INPUT_SAMPLERATE }
    

    is a compound literal. Check more about it here.

    From the C11, chapter §6.5.2.5

    A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.

    That said, I do not see how the braces are not balanced here. This is a valid syntax and your tool should consider this while making decision.