Pelles C forum

C language => Windows questions => Topic started by: ly47 on June 15, 2014, 06:56:24 PM

Title: Petzold exaples with PellesC
Post by: ly47 on June 15, 2014, 06:56:24 PM
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



Title: Re: Petzold exaples with PellesC
Post by: frankie on June 15, 2014, 07:12:25 PM
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....
Title: Re: Petzold exaples with PellesC
Post by: ly47 on June 15, 2014, 08:05:15 PM
Hi frankie
Thanks a lot, now it works.
ly
Title: Re: Petzold exaples with PellesC
Post by: neo313 on June 15, 2014, 08:14:56 PM
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 ;
Title: Re: Petzold exaples with PellesC
Post by: ly47 on June 15, 2014, 11:37:06 PM
Hi neo313
Thanks for your explanation.
ly
Title: Re: Petzold exaples with PellesC
Post by: frankie on June 16, 2014, 09:13:52 AM
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???
Title: Re: Petzold exaples with PellesC
Post by: neo313 on June 16, 2014, 10:43:11 AM
Quote from: ly47 on June 15, 2014, 11:37:06 PM
Hi neo313
Thanks for your explanation.
ly
No problem,

neo