Thursday, January 27, 2011

Pointers and qualifiers

I had forgotten what the rules for pointer qualifiers were, so for the ignorant and forgetful, here they are:

The stuff to the left of the * is what the pointer points at.
The stuff to the right of the * is the qualifiers on the pointer itself.

So... volatile char* volatile * const ptr
is... a pointer that can't be changed, that points to a volatile pointer that points to volatile chars
 
Woo!

3 comments:

  1. Never thought of it that way... that's a pretty nifty mnemonic!

    ReplyDelete
  2. If you get into the habit of writing your qualifiers on the right (which I have to admit I usually don't do myself), then definitions like this can be easily read simply by tokenising then scanning right to left. So, your original type:

    "volatile char* volatile * const"

    .. can be rewritten as the functionally-identical:

    "char volatile * volatile * const"

    Then, when this is read from right to left, it is quite obviously a constant pointer to a volatile pointer to a volatile character. Et voila! ;)

    ReplyDelete
  3. Its long when I stopped coding in c So almost forgot the concepts of pointers and how they are used and syntax.But your post helped me to recollect all the facts.Pointers are always a confusing until you have some application with proper use of pointers

    ReplyDelete