3

Possible Duplicate:
what is the difference between const int*, const int * const, int const *

I was reading FLTK code when I bumped into this line of code:

Fl_Widget*const* a = array();

here is the actual code:

Fl_Widget*const* Fl_Group::array() const {
    return children_ <= 1 ? (Fl_Widget**)(&array_) : array_;
}

int Fl_Group::find(const Fl_Widget* o) const {
    Fl_Widget*const* a = array();
    int i; for (i=0; i < children_; i++) if (*a++ == o) break;
    return i;
}

Now I'm wondering what is the type of pointer variable a. Are Fl_Widget*const* a = array(); and Fl_Widget** const a = array(); equal?

Community
  • 1
  • 1
sepisoad
  • 2,201
  • 5
  • 26
  • 37
  • There is an existing post that covers this (and many more combinations of pointer and const): http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const – jogojapan Dec 27 '12 at 09:56
  • You can use the [right-left rule](http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html) to "deciper" this. – Bhargav Dec 27 '12 at 09:57
  • Use [cdecl](http://cdecl.ridiculousfish.com/?q=int*const*+a). – Fred Foo Dec 27 '12 at 10:18

1 Answers1

3

You read it right-to-left:

Fl_Widget      *            const            *              a
          "pointer to" <- "constant" <- "pointer to"  <- "a is"

Which sums up to "a is a pointer to constant pointer to Fl_Widget".

Declarations in VAR a: POINTER TO CONST POINTER TO Fl_Widget style would have been a bit more clear, but C++ drags his variable declaration syntax from C, and C was all about expressions, not data types. Heck, it didn't even have const word, so you didn't have to think about it, and int *a, b was obviously deciphered as "*a is int, and b is int".

Joker_vD
  • 3,715
  • 1
  • 28
  • 42