2

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Vilat
  • 23
  • 2
  • 2
    K&R is too old to properly teach standard C. Compound literals were introduced in the year 1999. Consider getting a book which isn't older than 20 years. – Lundin Nov 21 '19 at 07:52

1 Answers1

6

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    Thanks @bath, Added one more. – Sourav Ghosh Nov 21 '19 at 07:48
  • braces are balanced here. What I meant is when "{" or "}" is encountered "(", ")" and "[" ,]" must be balanced. That was just a guess. – Vilat Nov 21 '19 at 07:56
  • 1
    Some older syntax highlighting tools like `vim` are confused by the presence of curly braces within round parentheses `({})`. It will flag all such opening braces as an error. I don't know whether the current version of `vim` fixes this, but the one I'm using still has this problem. For me, it's just a nuisance; for someone not as versed in C syntax, it must be freaking them out. – cmaster - reinstate monica Nov 21 '19 at 08:21