Click here to Skip to main content
15,499,571 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 22:18
professionalfd975018-Nov-20 22:18 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch19-Nov-20 3:22
mvahoney the codewitch19-Nov-20 3:22 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
ColinBurnell18-Nov-20 23:26
professionalColinBurnell18-Nov-20 23:26 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch19-Nov-20 3:21
mvahoney the codewitch19-Nov-20 3:21 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Stuart Dootson19-Nov-20 1:39
professionalStuart Dootson19-Nov-20 1:39 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Member 1330167919-Nov-20 3:56
MemberMember 1330167919-Nov-20 3:56 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch19-Nov-20 4:01
mvahoney the codewitch19-Nov-20 4:01 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Member 1330167919-Nov-20 10:11
MemberMember 1330167919-Nov-20 10: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 4:27
MemberRusty Bullet19-Nov-20 4:27 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
NightPen19-Nov-20 6:29
MemberNightPen19-Nov-20 6:29 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
Jan Heckman20-Nov-20 13:02
professionalJan Heckman20-Nov-20 13:02 
GeneralRe: I love C where types are basically a suggestion rather than a rule Pin
honey the codewitch20-Nov-20 13:22
mvahoney the codewitch20-Nov-20 13:22 
GeneralUseless IoT Pin
David O'Neil18-Nov-20 6:45
professionalDavid O'Neil18-Nov-20 6:45 
GeneralRe: Useless IoT Pin
honey the codewitch18-Nov-20 6:57
mvahoney the codewitch18-Nov-20 6:57 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 7:10
mveOriginalGriff18-Nov-20 7:10 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 7:17
mveOriginalGriff18-Nov-20 7:17 
GeneralRe: Useless IoT Pin
honey the codewitch18-Nov-20 7:19
mvahoney the codewitch18-Nov-20 7:19 
GeneralRe: Useless IoT Pin
David O'Neil18-Nov-20 7:26
professionalDavid O'Neil18-Nov-20 7:26 
GeneralRe: Useless IoT Pin
Stuart Dootson19-Nov-20 1:41
professionalStuart Dootson19-Nov-20 1:41 
GeneralRe: Useless IoT Pin
enhzflep20-Nov-20 2:38
Memberenhzflep20-Nov-20 2:38 
GeneralRe: Useless IoT Pin
honey the codewitch20-Nov-20 2:42
mvahoney the codewitch20-Nov-20 2:42 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 6:57
mveOriginalGriff18-Nov-20 6:57 
GeneralRe: Useless IoT Pin
Rick York18-Nov-20 7:01
mveRick York18-Nov-20 7:01 
GeneralRe: Useless IoT Pin
Maximilien18-Nov-20 7:26
MemberMaximilien18-Nov-20 7:26 
GeneralRe: Useless IoT Pin
OriginalGriff18-Nov-20 9:10
mveOriginalGriff18-Nov-20 9: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.


Straw Poll

When thinking about your job, how important is the option to work from home?
Some bosses are casual about where you work, and some, naming no names, will insist you're in the office, always. What are your thoughts?
  Results   773 votes