NO

Author Topic: Petzold exaples with PellesC  (Read 4709 times)

ly47

  • Guest
Petzold exaples with PellesC
« 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




Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Petzold exaples with PellesC
« Reply #1 on: June 15, 2014, 07:12:25 PM »
With a cast to void **:
Code: [Select]
hBitmap = CreateDIBSection (NULL, pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0) ;    //Line 104PellesC unlike MSVC makes strict types checking as required by C standards.
MSVC is well known to be very lazy about these issues....
« Last Edit: June 15, 2014, 07:15:09 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

ly47

  • Guest
Re: Petzold exaples with PellesC
« Reply #2 on: June 15, 2014, 08:05:15 PM »
Hi frankie
Thanks a lot, now it works.
ly

neo313

  • Guest
Re: Petzold exaples with PellesC
« Reply #3 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:

Code: [Select]
    void* temp = NULL ;
    hBitmap = CreateDIBSection (NULL, pbmi, DIB_RGB_COLORS, &temp, NULL, 0) ;
    pBits = temp ;

ly47

  • Guest
Re: Petzold exaples with PellesC
« Reply #4 on: June 15, 2014, 11:37:06 PM »
Hi neo313
Thanks for your explanation.
ly

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Petzold exaples with PellesC
« Reply #5 on: June 16, 2014, 09:13:52 AM »
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

  • Guest
Re: Petzold exaples with PellesC
« Reply #6 on: June 16, 2014, 10:43:11 AM »
Hi neo313
Thanks for your explanation.
ly
No problem,

neo