Greg-

>"const void ** dataP" means that the value pointed to by dataP can not be
>changed.  Please consider this carefully.  It does not mean that the stuff
>the pointer that dataP points to can't be changed, but that the value of
>dataP can't be changed.  Perhaps an example will help (we'll deal with
>char's instead of voids for clarity):

This is EXACTLY what I want. dataP is a pointer to the data I want to 
access READ-ONLY; thus I WANT the compiler to prevent me from 
modifying *dataP. For instance, in memory, there is:

0x0010  "Alan Pinstein"


Thus I want to access that via a C api, but force the situation such 
that the data integrity of "Alan Pinstein" is not compromised:

char*   dataP;
DBGetCellPtr_RO(... &dataP);
DrawString(dataP);      // the value of dataP is now 0x0010; the 
value of *dataP is 'A'. I don't want to be able to do this now:
*dataP = 'B';

had I done:

const char*     dataP;
DBGetCellPtr_RO(... &dataP);
DrawString(dataP);      // the value of dataP is now 0x0010; the 
value of *dataP is 'A'.
*dataP = 'B';   // illegal statement

My whole problem is setting up the declaration to not allow the first 
block of code to compile...

I tried your declaration of:

        void * const * dataP

but it just doesn't work at all. it doesn't even allow me to SET the 
return value of *dataP; I am not exactly sure what void * const * 
dataP actually declares. Tricky const declarations are one aspect of 
C I have yet to master....


Alan Pinstein
Synergy Solutions, Inc.
http://www.synsolutions.com
1-800-210-5293



>Alan,
>
>At 08:38 AM 8/26/99 -0500, you wrote:
> >dbErrType DBGetCellPtr_RO(DBOpenDBInfoType* gdb, char* colName, ULong
> >row, ULong* dataSize, const void** dataP);
> >// the const declaration makes this line illegal:
> >**dataP = 5; // can't assign data to const
>
>You have fallen into the dreaded const-declaration trap.
>
>"const void ** dataP" means that the value pointed to by dataP can not be
>changed.  Please consider this carefully.  It does not mean that the stuff
>the pointer that dataP points to can't be changed, but that the value of
>dataP can't be changed.  Perhaps an example will help (we'll deal with
>char's instead of voids for clarity):
>
>   char* data = "A string";
>   const char** dataP = &data;
>
>   // Legal stuff:
>   **dataP = '\0';  // data now equals ""
>   *data = 'T';      // data is "A string" again.
>   data = "Another String";  // this is fine - the value of data (ie what
>it points to) can change.
>
>   // Let's break the law:
>   *dataP = "First string";  // This is *illegal* - the value of (*dataP)
>is const!
>
>This is almost certainly not what you want.  So let's figure out what you
>do want:
>
>First, we want a pointer to constant data (we'll go back to voids here):
>
>   const void*
>
>We can also write this as
>
>   void const*
>
>While less usual and a bit awkward, this is actually easier to figure out -
>reading right to left we get a pointer to constant void.
>
>So, if we want a pointer to this, we want:
>
>   void const **
>
>Or a pointer to a pointer to constant void.
>
>Reworking your declaration, we'd get
>
>   void * const *
>
>Which is a pointer to a constant pointer to void.
>
>I hope this is clear.  This is one of the more difficult nuances of 'C'
>declarations, and I myself have had to rewrite this several times to make
>sure I got it right.  In fact, when I ran the above sample through VC6, it
>choked
>
>   const char** dataP = &data;
>
>Metrowerks seemed to handle it better.
>
>Anyhow, that's my understanding.  I'm sure someone will come along and
>debate it; but if this works I'll take those beers!
>
>Regards,
>Greg
>
>Greg Winton
>Bachmann Software and Services, LLC
>mailto:[EMAIL PROTECTED]
>http://www.bachmannsoftware.com
>Software Development for Handheld & Mobile Computing, Windows and the Internet
>Home of Bachmann Print Manager, the only graphical printing solution for
>the Palm Computing Platform

Reply via email to