Petzold exaples with PellesC

Started by ly47, June 15, 2014, 06:56:24 PM

Previous topic - Next topic

ly47

Hi

I'm transferring examples from Petzold Book to be compiled with PellesC.
(There are 23 chapters, so far I did only 15 chapters).
There is a problem with chapter 15- DibSect  example.
I'm getting this error message:
//
C:\C\Projects\Petzold\p15-DibSect\DibSect.c(104): error #2140: Type error in argument 4 to 'CreateDIBSection'; expected 'void * *' but found 'unsigned char * *'.
*** Error code: 1 ***
//
How to fix it ?
Thanks
ly




frankie

#1
With a cast to void **:
hBitmap = CreateDIBSection (NULL, pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0) ;    //Line 104
PellesC unlike MSVC makes strict types checking as required by C standards.
MSVC is well known to be very lazy about these issues....
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

ly47

Hi frankie
Thanks a lot, now it works.
ly

neo313

This is a good time to make sure your code adheres to the c standard. While Pelles and VisualStudio don't follow aliasing rules, other compilers might( gcc does ), causing your code to produce undefined behavior on such compilers.

If a function requires a void** type, then the only standard compatible way is to pass that type. Types char** and void** are not compatible, such types may not alias, even with the cast.

So the correct solution would be:


    void* temp = NULL ;
    hBitmap = CreateDIBSection (NULL, pbmi, DIB_RGB_COLORS, &temp, NULL, 0) ;
    pBits = temp ;

ly47

Hi neo313
Thanks for your explanation.
ly

frankie

Quote from: neo313 on June 15, 2014, 08:14:56 PM
If a function requires a void** type, then the only standard compatible way is to pass that type. Types char** and void** are not compatible, such types may not alias, even with the cast.

What means???
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

neo313