9

What's the difference between the following three pointer declarations in C:

void * const myPointer1;
void const *myPointer2;
const void *myPointer3;

And which one is used to prevent:

myPointer = somethingElse;
lmirosevic
  • 15,787
  • 13
  • 70
  • 116

4 Answers4

10

Read the rules from right to left:

void * const myPointer1;

myPointer1 is a const pointer to void.

void const *myPointer2;

myPointer2 is a pointer to a const void.

const void *myPointer3;

myPointer3 is a pointer to a void const.

Conclusions:

  • myPointer1 is what you are looking for -- it's a const pointer, so its value cannot be modified
  • myPointer2 and myPointer3 are the same thing
  • myPointer2 and myPointer3 are kind of meaningless -- dereferencing a void* does not make sense
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Your type 2 and 3 (`const void *`) are very meaningful and required if you want to assign from another `const` pointer, such as `const char * s = "..."; const void * vp = s;` – Hudson Jun 12 '20 at 13:12
1

In some places, you can put the const in front of whatever is declared const:

const int * x;  // pointer to constant int

You can always put the const after whatever is declared const:

int const * x;       // pointer to constant int
int * const x;       // constant pointer to int
int const * const x; // constant pointer to constant int

Hence, my personal recommendation, always have the const trailing, because that's the only "rule" that can be adhered to consistently.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0
  • myPointer1 is a const pointer to void.
  • mypointer2 and myPointer3 are both pointers to const void.

The difference between myPointer2 and myPointer3 declarations is just a matter of style.

NB: const void means here that the pointed data is const. Nothing to do with void from int main(void) for instance.

md5
  • 23,373
  • 3
  • 44
  • 93
  • What would be the meaning of `void` type (or `const void` for that matter)? E.g., what can be assigned into the address pointed by such pointer (`*mypointer2 = ?`) – SomeWittyUsername Mar 01 '13 at 11:57
  • @icepack: A `void *` is a "generic pointer", i.e. a "pointer to whatever". You would assign another pointer's value to it, like `mypointer = otherpointer`. You wouldn't assign value to the *dereferenced* pointer, though. – DevSolar Mar 01 '13 at 12:02
  • @DevSolar Yes, I'm aware of that. I was wondering about the semantic meaning of "pointer to void". Sounds to me that such a description is logically incorrect. `void` here is an alias for "anything" in contrast to a normal meaning of `void`. – SomeWittyUsername Mar 01 '13 at 12:09
  • @icepack: No, it's a "void pointer": The pointer itself exists alright, but it doesn't point to anything (useable). You can cast it to a different pointer type, and use *that* for further processing. (See `malloc()`.) So the use of "void" isn't as misled as it might seem. – DevSolar Mar 01 '13 at 12:50
0
  1. void * const myPointer1; = declare myPointer1 as const pointer to void
  2. void const *myPointer2; = declare myPointer3 as pointer to void const
  3. const void *myPointer3; = declare myPointer3 as pointer to const void

Whenever in such kinda doubts, you can use:: cdecl.org

You should try myPointer1 to avoid the condition you explained as it is a const pointer.

Abhineet
  • 5,320
  • 1
  • 25
  • 43
  • BTW. http://cdecl.org/ doesn't support the "void const *myPointer2;". Which is why I landed on this page, googling to confirm what I remembered, that way 2 is the same as way 3. – domen Jun 20 '13 at 09:25