0

So I have the following expression

int *(*table())[30];

In my opinion table() return a value which points to the beginning of an array of pointers which each element points to an integer.

What do you think?

Thanks.

Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
Assaf Malki
  • 285
  • 2
  • 3
  • 9

1 Answers1

5

You're correct. According to cdecl

int *(*table())[30];

declare table as function returning pointer to array 30 of pointer to int

See also the clockwise/spiral or right-left rules for help understanding C expressions (and see comments below for some points in favour of the latter).

simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    I wouldn't recommend spiral rule. Because it is 1) overcomplicated and 2) wrong (try to parse `int a[4][5]` for example). There's more simple and reliable rule: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html – kotlomoy May 25 '13 at 16:35
  • 1
    @kotlomoy you need to insert the implicit parentheses for the spiral rule, `int (a[4])[5];`, then it works, as far as I know. – Daniel Fischer May 25 '13 at 17:59
  • @DanielFischer What implicit parentheses? There are no parentheses there. Nor implicit nor explicit. Of course you can *modify* declaration to allow spiral rule to parse it, but why? It's just beyond my understanding... Right-left rule works fine here and everywhere else. Because, basically, it's how compiler parses declarations. Also, it's much simpler. What's special with this spiral rule? I don't understand really – kotlomoy May 25 '13 at 18:17
  • @kotlomoy The implicit parentheses you have because the `[4]` is associated to `a` and not to `[5]`. Whenever you have a declaration with more than two parts (type and identifier), you have to associate in some way, that inserts implicit parentheses where no explicit parentheses are. It's necessary to use the implicit parentheses for the spiral rule to work, so if you want to use the spiral rule, you have a good reason to insert them, haven't you? [I'm not advocating to use the spiral rule, if you thought that.] – Daniel Fischer May 25 '13 at 18:23
  • 2
    Rather the contrary, if you have to do something unusual to use it, that's a point against using it, I'd think. – Daniel Fischer May 25 '13 at 18:25
  • @DanielFischer What you say about is *order of precedence*. Spiral rule breaks this order, that's why you need to insert superfluous parentheses to make this rule work. This is the reason why I'm advocating against spiral rule. – kotlomoy May 25 '13 at 18:34
  • 1
    @kotlomoy Thanks for your comments. I've added a link to the right-left rule to my answer. – simonc May 25 '13 at 18:44
  • 1
    @kotlomoy The whole point of precedence is to make some parentheses implicit. So where I said "implicit parentheses", I could have spoken of precedence. But "You can use the spiral rule if you deviate from it when precedence demands it." doesn't sound as snappy as "It works when you insert the implicit parentheses.", so I prefer to use the latter to make a pun. – Daniel Fischer May 25 '13 at 18:55