0

When i use the spiral rule, i am confused at below line within 10 spiral steps. Is there a quicker way?

// compiles in VC++ 2010
const void * const ** const volatile ***  const **** _foo_; 

Such as ptr is a pointer to a pointer to a pointer to a pointer and all of them are const void but 2 of them are volatile void const

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97

4 Answers4

13

cdecl says:

const void * const ** const volatile ***  const **** p

declare p as pointer to pointer to pointer to pointer to const pointer to pointer to pointer to const volatile pointer to pointer to const pointer to const void

Griwes
  • 8,805
  • 2
  • 43
  • 70
Paul R
  • 208,748
  • 37
  • 389
  • 560
8

This is easy with spiral rule!

enter image description here

:-P

jrok
  • 54,456
  • 9
  • 109
  • 141
  • Now add `[][]` to the right, and see what the "spiral rule" gives you. There is no spiral rule which works. – James Kanze Aug 13 '12 at 14:28
  • @JamesKanze, easy - start with name, then go clockwise to encounter `[][]` being two-dimensional array of -> use spiral rule again. – Griwes Aug 14 '12 at 13:47
  • @Griwes In other words, redefine spiral to block elements according to the parentheses. Given something like `**name[][]` is array of pointer to array of pointer to. The rule is right, then left, respecting parentheses. – James Kanze Aug 14 '12 at 14:07
7

With pointer and reference declarations, you just read from right to left:

ptr is a pointer to a pointer to a pointer to a pointer to a const pointer to a pointer to a pointer to a pointer to a const volatile pointer to a pointer to a const pointer to a const object of unknown type.

If you follow the guideline of putting any const or volatile qualifiers after the first type specifier (i.e. void const *) rather then before it (i.e const void *), then you can read consistently from right to left; otherwise, you sometimes have a slight hiccough when you reach the left-hand end.

The spiral "rule" is occasionally useful for declarations involving arrays or functions, where the name being declared isn't the last thing in the declaration. In this case, with nothing after the name, it degenerates to reading right-to-left.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
5

That's because there is no spiral rule. Basically, you process operands on right first, then on the left, working outward in both cases, and respecting parentheses. And cv-qualifiers normally qualify what's to the left of them. Since this declaration has no operands on the right, it's simply right to left: pointer to pointer to pointer to pointer to const pointer to pointer to pointer to const volatile pointer to pointer to const pointer to (const) void. The last const is because the final const doesn't have anything to the left, so we have to treat the declaration as if it were void const, instead of const void. Other than that, the declaration should cause no problems, if you forget about the misguided spirals.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • +1, I also tend to follow the rule of writing `const` to the right of the type as it makes reading more consistent (at least in my spare time, at work people don't really care for that style). – David Rodríguez - dribeas Aug 13 '12 at 14:20