Click here to Skip to main content
15,884,388 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
fd975018-Nov-20 21:18
professionalfd975018-Nov-20 21:18 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch19-Nov-20 2:22
mvahoney the codewitch19-Nov-20 2:22 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
ColinBurnell18-Nov-20 22:26
professionalColinBurnell18-Nov-20 22:26 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch19-Nov-20 2:21
mvahoney the codewitch19-Nov-20 2:21 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Stuart Dootson19-Nov-20 0:39
professionalStuart Dootson19-Nov-20 0:39 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Member 1330167919-Nov-20 2:56
Member 1330167919-Nov-20 2:56 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch19-Nov-20 3:01
mvahoney the codewitch19-Nov-20 3:01 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Member 1330167919-Nov-20 9:11
Member 1330167919-Nov-20 9:11 
Quote:
byte* bp = (byte*)&s;


That's a bug :-/. C does not allow conversion of pointers to anything other than a void pointer and back again. A compiler may let you do it but the language standard calls it a bug (undefined behaviour).

In other words, that isn't correct C; many compilers will allow it and let the program produce unpredictable results (anything from producing the correct results, producing incorrect results, all the way to crashing).

The correct way to do that is to use a union, at which point the strong typing gets enforced and alignment is guaranteed and you have not broken any of the C standards rules. While this way isn't Undefined Behaviour, it's not fully defined either - it's Implementation Defined (someone will correct me if I am wrong on this point, no doubt Smile | :) due to padding that may or may not occur depending on what flags were given to the compiler.

To be honest, any time you need to put in a cast in C (not C++) because the compiler is complaining, it's probably a bug. There are very few uses of casting in C (not C++) that aren't bugs. All uses of casting that I've seen in production code were alignment bugs. The few cases you need casting in C is when you are writing generic container functions and want to provide const guarantees to the callers.

Also, 'byte' is not a type in C. You probably mean to use 'uint8_t' or 'uint_least8_t'. I've seen programs use 'char' for bytes, but that isn't correct either as the standard doesn't require 'char' to be unsigned or to be signed (it's left up to the implementation) and 'working' code will suddenly stop working if the program is recompiled on a compiler which defaults to signed char (left bitshift operations on signed integer types are undefined. It's only defined for unsigned integer types).

I've been seeing more and more of this meme "C has weak typing" in various forums recently. In reality there is literally only one or two specific instances where the typing breaks down in C.
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Rusty Bullet19-Nov-20 3:27
Rusty Bullet19-Nov-20 3:27 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
NightPen19-Nov-20 5:29
NightPen19-Nov-20 5:29 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Jan Heckman20-Nov-20 12:02
professionalJan Heckman20-Nov-20 12:02 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch20-Nov-20 12:22
mvahoney the codewitch20-Nov-20 12:22 
GeneralUseless IoT Pin
David O'Neil18-Nov-20 5:45
professionalDavid O'Neil18-Nov-20 5:45 
GeneralRe: Useless IoT Pin
honey the codewitch18-Nov-20 5:57
mvahoney the codewitch18-Nov-20 5:57 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 6:10
mveOriginalGriff18-Nov-20 6:10 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 6:17
mveOriginalGriff18-Nov-20 6:17 
GeneralRe: Useless IoT Pin
honey the codewitch18-Nov-20 6:19
mvahoney the codewitch18-Nov-20 6:19 
GeneralRe: Useless IoT Pin
David O'Neil18-Nov-20 6:26
professionalDavid O'Neil18-Nov-20 6:26 
GeneralRe: Useless IoT Pin
Stuart Dootson19-Nov-20 0:41
professionalStuart Dootson19-Nov-20 0:41 
GeneralRe: Useless IoT Pin
enhzflep20-Nov-20 1:38
enhzflep20-Nov-20 1:38 
GeneralRe: Useless IoT Pin
honey the codewitch20-Nov-20 1:42
mvahoney the codewitch20-Nov-20 1:42 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 5:57
mveOriginalGriff18-Nov-20 5:57 
GeneralRe: Useless IoT Pin
Rick York18-Nov-20 6:01
mveRick York18-Nov-20 6:01 
GeneralRe: Useless IoT Pin
Maximilien18-Nov-20 6:26
Maximilien18-Nov-20 6:26 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 8:10
mveOriginalGriff18-Nov-20 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.