Syntax of Structs and Unions in C

  Рет қаралды 9,631

HackerCS

HackerCS

Күн бұрын

Пікірлер: 8
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
A great use of a union is dealing with arrays as named values and also as indexed arrays: union { struct { int monHrs, tueHrs, wedHrs, thuHrs, friHrs; } wk; int hrs[5]; } data; Code can then clearly refer to the specific "data.wk.thuHrs", for example, OR it can, for instance, sum all values: for( int i = 0; i < sizeof(data)/sizeof(int); i++) total += data.hrs[ i ]; Of course, #define'd index names (or enum tokens) would work the same, but one gets tired of seeing "v[ INDEXNAME ] = 12;" vs "u.v.realname = 12"...
@grimvian
@grimvian Жыл бұрын
Very rare and fantastic video explaining from documentation. I really hope he would make videos about advanced C... At 9.52 Almost the way I use a union to change RBG colors for my C training.
@abhishekgupta-du1ly
@abhishekgupta-du1ly Жыл бұрын
It very really helpful for me 😊
@David-gu8hv
@David-gu8hv 4 жыл бұрын
You have to watch out for the size of Structs. If you have a struct with one char and one int on a 32-bit system the struct will be 8 bytes since the smallest amount of memory on 32-bits is 4 bytes. The char will claim 4 bytes even though it only uses 1.
@nguaial8490
@nguaial8490 8 жыл бұрын
Thanks very much. This clarified a lot. I am assuming that the size of int and size of float are the same in this UNION example.
@mecrajib
@mecrajib 4 жыл бұрын
Many thanks.
@zerodept
@zerodept 6 жыл бұрын
Nice explanation of Unions
@franciscoabusleme9085
@franciscoabusleme9085 5 жыл бұрын
Excellent
Reading and Writing C Type Declarations Part 1/2
11:49
HackerCS
Рет қаралды 11 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 304 М.
Running With Bigger And Bigger Feastables
00:17
MrBeast
Рет қаралды 210 МЛН
PEDRO PEDRO INSIDEOUT
00:10
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 27 МЛН
What does int argc, char* argv[] mean?
10:11
Paul Programming
Рет қаралды 351 М.
Advanced C Coding For Fun!
46:53
Linux.conf.au 2011 -- Brisbane, Australia
Рет қаралды 92 М.
C: malloc and functions returning pointers
38:14
Joe McCullough
Рет қаралды 103 М.
Memory leak in C/C++
17:55
mycodeschool
Рет қаралды 243 М.
Header files and libraries (Kevin Lynch)
9:32
Northwestern Robotics
Рет қаралды 95 М.
How computer processors run conditions and loops
17:03
Core Dumped
Рет қаралды 76 М.
Reading and Writing C Type Declarations Part 2/2
8:52
HackerCS
Рет қаралды 7 М.
21:Everything u need 2 know about pointers -Richard Buckland
48:29
UNSW eLearning
Рет қаралды 537 М.
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 407 М.