Morris Keesan
6/12/2011 4:42:00 PM
[most newsgroups trimmed from reply]
On Sat, 11 Jun 2011 20:07:57 -0400, Skybuck Flying wrote:
> Hello,
>
> I come across the following C/C++ code which is a bit unfamiliar to me:
Which is it? C or C++? They're two different languages.
My answer assumes that it's C code.
>
> const void *SomeField2; // *
>
> typedef struct SomeStructureName1 {
> int SomeField1;
> const void *SomeField2; // *
> int SomeField3;
> } SomeStructureName2;
>
> It seems like a "constant void pointer type"
(void *) means a generic pointer to data, which can be converted to or
from a pointer to any object type.
(const void *) means the same kind of pointer, but it points to
non-modifiable data. So if if SomeField2 actually points to an (int),
then you can write
const int *intp = SomeField2;
giving you a pointer to an int which you can't modify, but you can't
write
int *intp = SomeField2;
because that discards the "const" modifier. When the keyword "const"
appears in the position shown above, it refers to the pointed-to data.
If SomeField were itself a constant (unmodifiable) field, then the
declaration would be written "void * const SomeField2;".
--
Morris Keesan -- mkeesan@post.harvard.edu