1

can some one interpret the below line:

const void *const *ptr; 

is it both the type & Ptr variable is constant?

danny
  • 1,587
  • 2
  • 12
  • 12
  • 6
    You might find the [spiral rule](http://c-faq.com/decl/spiral.anderson.html) helpful. – chris Aug 13 '12 at 13:38
  • 1
    That [declares ptr as pointer to const pointer to const void](http://cdecl.ridiculousfish.com/?q=const+void+*const+*p%3B) – Shawn Chin Aug 13 '12 at 13:41
  • possible duplicate of [What does this C statement mean?](http://stackoverflow.com/questions/8249483/what-does-this-c-statement-mean) – Alexey Frunze Aug 13 '12 at 13:45

2 Answers2

2

After

const void *const *ptr;

You can change ptr, but not *ptr, or **ptr

ptr = <SOMETHING_ELSE>; /* ok */
*ptr = <SOMETHING_ELSE>; /* error */
**ptr = <SOMETHING_ELSE>; /* error, ignoring the point that you cannot even have an object of type void */
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 2
    `const` or not, well you'll have an error dereferencing the pointers anyway (`**ptr`), because it's a pointer to an incomplete type. ;-) – netcoder Aug 13 '12 at 14:07
1

From the top of my mind:

void * const var;    // The pointer is constant and var can change
const void * var;    // The pointer can change but not var

so I would think that your syntax

const void * const *ptr;

means that ptr is a pointer to a pointer. So ptr would point to an address and that address cannot change (the first const). Also the address that ptr is located cannot change (the second const). But I am not totally sure of this.

rpsml
  • 1,486
  • 14
  • 21