Nils
In case?
It depends on compiler behavior.
Nils
With some compilers it might break
SetPf
It depends on compiler behavior.
Than nvm. This is obviously made for only one compiler.
Pavel
It depends on compiler behavior.
If it's not for type punning (doesn't seem to be), then what can be the issue?
SetPf
serial protocol parsing
Nils
It simply is not defined by the standard
Nils
Means undefined behavior
Pavel
It is
Well in this case I agree
Nils
Might also vary between standards
But I don't think it is defined by any standard
Vlad
Especially that delete is nasty
Vlad
If you want to type pun use memcpy
Vlad
You won't die by copying 4 bytes over. But your code will be free of ub
Nils
Lol
Vlad
No, just cast
I guess in this case he can just reinterpret cast. Cause all members are uint
Nils
Yeah
Nils
Ohhhh so that's what static_cast is for?? Changing signednes? 🤯
Vlad
Yeah
But you can't do that in general case
Anonymous
Even looking at it hurts! 😁
like a heartble https://en.wikipedia.org/wiki/Heartbleed
SetPf
You won't die by copying 4 bytes over. But your code will be free of ub
i need to push unknown bytes, one by one, in struct, and then, after some movements in memory, take a value of certain type. I dont know more good way to make it only with array in uinon (as i always made in C)
SetPf
is this is right? struct Value { private: size_t arr_size = 0; uint8_t array[sizeof(size_t)]; public: void PushByte(uint8_t i) { array[arr_size++]=i; } uint32_t asuint(void) { return *reinterpret_cast<uint32_t>(array); } int16_t asint16(void) { return *reinterpret_cast<int16_t>(array); } }
SetPf
So much so. But array size is unnecessary
Just ptr++ would be ok for both big and little endians? I never think about how ptrs behaiving in this cases.
Vlad
struct Value { private: uint8_t data[sizeof(size_t)]; public: uint8_t& operator[](size_t i) { return data[i]; } }
Vlad
I mean why the size of size_t
Vlad
Pls explain the last string =) As i understand overloading but for and why?
You allow user of this partucal struct to access it as array
SetPf
You allow user of this partucal struct to access it as array
I must not encapsulate assembling and casting in it?
SetPf
Nils
Explain pls. About const.
Const means the function won't change any class member so const functions are still callable for const instances
Nils
Why inline?
Performance, compiler will optimise away pointer if possible.
Anonymous
Now make the functions inline and const
They are inlined by default
Pavel
Member functions are
Nils
They are inlined by default
Class members? What? Oh okay lol
Anonymous
Performance, compiler will optimise away pointer if possible.
inline doesn't make compiler to inline a function
Anonymous
Class members? What? Oh okay lol
If they are defined in a class body
Nils
Sam
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { char ch; char s[100]; char sen[100]; printf("\nEnter char"); scanf("%c",&ch); printf("Enter word"); scanf("%s",&s); printf("Enter sentence\n"); fgets(sen,sizeof(sen), stdin); printf("%c\n",ch); printf("%s\n",s); puts(sen); return 0; }
Sam
Why isn't the fgets function working
Sam
Pls help
Al
fgets (sen, strlen(sen)+1,stdin)
Al
sam question sorry
Sam
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { char ch; char s[1000]; char m[1000]; scanf("%ch\n",&ch); scanf("%s\n",&s); fgets(m,sizeof(m), stdin); printf("%c\n",ch); printf("%s\n",s); puts(m); return 0; }
Pavel
Ah I see. Good to know.
Function templates also (except for full specializations)
Sam
Over here it's working fine
Al
what is the error?
Sam
what is the error?
No error ....it's not reading sentence
Al
try gdb?
Sam
I tried it
Sam
Can you pls try it once...and send the correct code if possible
Sam
I tried it many times
Al
yes but im in the car and using only phone atm 😄
Sam
Ohh okay...will you pls send it later
Al
ok ok
Sam
Thanks :)
Al
no
Al
np
Al
@sam_1208 you need to fflush(stdin) before fgets
Al
you scanf mess it up stdin
Al
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { char ch; char s[100]; char sen[100]; printf("\nEnter char"); scanf("%c",&ch); printf("Enter word\n"); scanf("%s",s); printf("Enter sentence\n"); fflush(stdin); fgets(sen,sizeof(sen), stdin); printf("%c\n",ch); printf("%s\n",s); puts(sen); return 0; }
Al
(done in the car via remote terminal)
Nils
(done in the car via remote terminal)
Don't program while driving a car.
Al
😂
Al
dont drink and drive
Beyond
wtf
Al
im just a passanger anyway
baydo
UML???
Anonymous
Why isn't the fgets function working
There was probably a newline char just before the line started, use fgets() for all the inputs and sscanf().