From François Pinard's notes |
||
CI used C for a lot of years and in a flurry of projects. I
selected it mainly for portability or speed considerations, that
is, for practical matters. As for the language itself, I found
its syntax repulsive at first (I had a strong Pascal background
at the time, and much enjoyed Pascal design and syntax). But in
the long run, I tamed myself to C in some deep way, and many
surrounding aspects (like Flex, Bison, Autoconf, Automake,
Gettext, and of course, coding standards). Contents 1 Operator precedenceA quick reminder chart. Each line gives operators sharing a
precedence level. The first line gives the most binding
operators, the last line gives the least binding operators. The
last column indicates if operators associate from left to right
(->) or from right to left (<-): 2 Reading declarationsI found the following description somewhere on the Net, a while ago, and saved it without reference (in a word: I did not write it). The right-left rule is a completely regular rule for deciphering C declarations. It can also be useful in creating them. 2.1 Reading rulesFirst, symbols. Read as you encounter them in the declaration.
2.2 A first example
1) Find identifier: int *p[]; p is 2) Move right until out of symbols or left parenthesis
hit: 3) Can't move right anymore (out of symbols), so move left and
find: p is array of pointer to 4) Keep going left and find: 2.3 Another example
func is 2) Move right: 3) Can't move right anymore because of the left parenthesis,
so move left: func is function returning pointer to 4) Can't move left anymore because of the right parenthesis,
so keep going func is function returning pointer to function returning 5) Can't move right anymore because we're out of symbols, so
go left: 6) And finally, keep going left, because there's nothing left
on the right: Whew! (Brief pause whilst the author wipes the sweat off his brow.) As you can see, this rule can be quite useful. You can also use it to sanity check yourself while you are creating declarations, and to give you a hint about where the put the next symbol and whether parentheses are required. 2.4 More complex casesSome declarations look much more complicated than they are due
to array sizes and argument lists in prototype form. If you see
[3], that's read as array (size 3) of ….
If you see (char *, int) that's read as
function expecting (char *, int) and returning ….
Here's a fun one: fun_one is pointer to function expecting (char *, double) and returning pointer to array (size 9) of array (size 20) of int. As you can see, it's not as complicated if you get rid of the
array sizes and argument lists: You can decipher it that way, and then put in the array sizes and argument lists later. 2.5 Some final wordsIt is quite possible to make illegal declarations using this
rule, so some knowledge of what's legal in C is necessary. For
instance, if the above had been: it would have been fun_one is pointer
to function returning array of array of
pointer to int. Since
a function cannot return an array, but only a pointer to an
array, that declaration is illegal. Last modified: 2009-10-15 05:45 |
||