Pointers to Structures in C - 9 Examples to Kickstart Your Journey

  Рет қаралды 2,927

Gary Explains

Gary Explains

Күн бұрын

Пікірлер: 16
@Psychlist1972
@Psychlist1972 4 ай бұрын
Nice intro video, Gary. C/C++ isn't taught much in school these days, at least not in intro courses like it used to be. Most start with Java. For folks following along: Keep in mind that adding up the sizeof elements *inside* the struct does not necessarily equal the total size of the struct. I know Gary chose values carefully here so what he walked through was valid for the sizes shown, not necessarily for others. sizeof(point_t) may be larger depending upon alignment. So if you change the string to be 61 characters, depending upon the compiler's default alignment settings, you'll still see sizeof is 72. If you change to be 69 characters, you end up with sizeof being 80 (on x64 Windows visual C++ with default alignment and no packing directives). The takeaway is to always use sizeof on the type you're working with, as shown in the video. Don't make assumptions from adding up sizes of fields in the struct.
@mulletronuk
@mulletronuk 4 ай бұрын
Great video as always Mr. Explains!
@GaryExplains
@GaryExplains 4 ай бұрын
Glad you liked it!
@nomadic_shadow
@nomadic_shadow 4 ай бұрын
Thanks, Gary. I'm working on my C chops so this is a greatly timed video for me.
@DavidAlsh
@DavidAlsh 4 ай бұрын
Hey Garry. great video! If I could make a request; a video series I'd love is one on Rust C/C++ FFI integration for developers who are familiar with Rust but not C. Use cases are things like writing bindings for C/C++ libraries, contributing to Linux (I guess?) and writing dynamic Rust libraries with a stable ABI I'm finding myself learning C just so I can understand the FFI types 😂
@juanmacias5922
@juanmacias5922 3 ай бұрын
Great video. :D not seeing the shorthand for *=, += was flaring up my pedantic nature ha but I'm sure you did it that way so it was a bit more readable. :) Hope this becomes a series.
@zrodger2296
@zrodger2296 4 ай бұрын
In the Linux kernel, as far as i know, they always use the 'struct' keyword, instead of defining their own type. I always wondered why that was. Thanks for the video!
@GaryExplains
@GaryExplains 4 ай бұрын
Yes, the Linux kernel coding standard forbids typedefs for structs as it says the typedef adds a layer of obscurity.
@jecelassumpcaojr890
@jecelassumpcaojr890 4 ай бұрын
I was a little surprised that you can assign structs in C. Checking the original K&R it said that wasn't allowed but it might change in the future, which it obviously has. An early paper on C (before K&R even) bragged about how in C you could always count on "q = p" taking a tiny amount of time unlike other languages that might hide some arbitrary complexity when compiling such an innocent expression. I guess priorities change.
@zrodger2296
@zrodger2296 4 ай бұрын
I still prefer a memcpy(), but I'm old fashioned! 😊
@GaryExplains
@GaryExplains 4 ай бұрын
From the 2nd edition of the C programming language: "The main change made by the ANSI standard is to define structure assignment - structures may be copied and assigned to, passed to functions, and returned by functions. This has been supported by most compilers for many years, but the properties are now precisely defined. Automatic structures and arrays may now also be initialized. "
@superangrybrit
@superangrybrit 4 ай бұрын
Very good video! 🙂
@GaryExplains
@GaryExplains 4 ай бұрын
Glad you liked it!
@4115steve
@4115steve 4 ай бұрын
something that is really confusing is when you used "a" twice in the function deceleration of the last example, a=a+b had me confused because why would a involve itself if it is the answer. I had to pause the video so I could place things in my mind where they needed to be inside of the void point add deceleration. it wasnt clear what void point add was returning. I suppose its returning another struct x and y. it was a great video and very informative, something that might add to another video like this is literally drawing out how things from int main move around into the function declaration at the top
@GaryExplains
@GaryExplains 4 ай бұрын
Your questions are good and that is the reason I included that last example. The function doesn't return anything. The key is that the pointer 'a' points to the block of memory where the first struct is stored. The data in the struct pointed to by 'b' is added directly to the data pointed to by 'a', hence a->x=a->x+b->x. When the function returns the memory at 'a' has been modified directly. 'b' remains unchanged. No need for a return of any kind.
@nathanaelsmith3553
@nathanaelsmith3553 4 ай бұрын
If you dont use 'clone' on DateTime objects in php you can end up with a pointer by mistake, and that can really eff you up.
you will never ask about pointers again after watching this video
8:03
M4 Deep Dive - Apple's First Armv9 Processor
15:20
Gary Explains
Рет қаралды 134 М.
didn't manage to catch the ball #tiktok
00:19
Анастасия Тарасова
Рет қаралды 34 МЛН
Smart Sigma Kid #funny #sigma
00:14
CRAZY GREAPA
Рет қаралды 64 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 269 #shorts
00:26
Sigma baby, you've conquered soap! 😲😮‍💨 LeoNata family #shorts
00:37
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 317 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 721 М.
Harder Drive: Hard drives we didn't want or need
36:47
suckerpinch
Рет қаралды 1,7 МЛН
OpenAi o1 is Amazing - It will have a HUGE Impact on EVERYONE in Tech
14:49
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 178 М.
Raspberry Pi Pico RP2350 - Massive Performance Boost Trick
12:34
Gary Explains
Рет қаралды 16 М.
3-Hour Study With Me | Pomodoro 60/10, No Music
3:36:10
K Dev
Рет қаралды 1,8 М.
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
didn't manage to catch the ball #tiktok
00:19
Анастасия Тарасова
Рет қаралды 34 МЛН