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.
@mulletronuk4 ай бұрын
Great video as always Mr. Explains!
@GaryExplains4 ай бұрын
Glad you liked it!
@nomadic_shadow4 ай бұрын
Thanks, Gary. I'm working on my C chops so this is a greatly timed video for me.
@DavidAlsh4 ай бұрын
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 😂
@juanmacias59223 ай бұрын
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.
@zrodger22964 ай бұрын
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!
@GaryExplains4 ай бұрын
Yes, the Linux kernel coding standard forbids typedefs for structs as it says the typedef adds a layer of obscurity.
@jecelassumpcaojr8904 ай бұрын
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.
@zrodger22964 ай бұрын
I still prefer a memcpy(), but I'm old fashioned! 😊
@GaryExplains4 ай бұрын
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. "
@superangrybrit4 ай бұрын
Very good video! 🙂
@GaryExplains4 ай бұрын
Glad you liked it!
@4115steve4 ай бұрын
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
@GaryExplains4 ай бұрын
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.
@nathanaelsmith35534 ай бұрын
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.