Re: Compiler error for "wrong" sized type



tedu <tu@xxxxxxxxxxxxx> wrote:
David White wrote:
Richard Heathfield wrote:
Well, I don't know about "better", but I like this:

char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 -
1];

If the struct is the wrong size, this will yield a negatively sized
array, which is illegal.

Thanks. That's an improvement. Unfortunately, our Hi-Tech compiler doesn't pick it up
because it treats an array size of -1 as unsigned!

does it allow multiple case statements with the same value?

switch (0) {
case sizeof(struct my_struct) == correct_size:
case 0:
}

Nit: a label reqires a following statement; make it:
switch (0) {
case sizeof(struct my_struct) == correct_size: ;
case 0: ;
}

(Otherwise it's a nice trick, I liked it.)

--
Stan Tobias
mailx `echo siXtY@xxxxxxxxxxxxxxxxxxxxxxxxxx | sed s/[[:upper:]]//g`
.



Relevant Pages

  • Re: [OT] Funny
    ... >> the NAG compiler does have a switch to allow you to tell it ... or the compiler simply isn't conforming. ... more arithmetic in computing addresses of array elements -- dis- ...
    (comp.lang.fortran)
  • SUMMARY: switch/hub for 3 T3 Raid devices
    ... > host and I want to use all 3 devices on the same host. ... Are there seperate hub and switch type solutions and is ... Loop from one array to another from the host to each array. ...
    (SunManagers)
  • Re: Whats the most efficient testing of variables?
    ... >>I need to quickly compare a DWORD with a list of DWORDs to ... >>switch won't work because both are variables. ... >linear search of an array ) will be faster. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: How to replace this switch with preprocessor code.... whats the advantage??
    ... its an array of structures ... containing configuration for each switch. ... After copying your code to the file test.c: ...
    (comp.lang.c)
  • Re: [PATCH 00/11] Task watchers: Introduction
    ... It needs to be done at the start of notify_watchers() before any other watchers are called for the new task. ... The notifier_block only needs to contain a pointer to the array so there's no increase in the size of that structure. ... It removes the need for a switch statement in the client's function as well as saving on unnecessary function calls. ...
    (Linux-Kernel)