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

Поделиться
HTML-код
  • Опубликовано: 19 июн 2024
  • The C programming language makes heavy use of pointers. Following on from my videos on "Pointers in C" and "Dynamic Memory Allocation in C - malloc, free, and buffer overflows" here is my tutorial on how to use pointers to structures, along with 9 practical examples.
    ---
    Pointers in C: • Pointers in C
    Dynamic Memory Allocation in C - malloc, free, and buffer overflows: • Dynamic Memory Allocat...
    Twitter: / garyexplains
    Instagram: / garyexplains
    #garyexplains
  • НаукаНаука

Комментарии • 25

  • @DavidAlsh
    @DavidAlsh 8 дней назад +2

    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 😂

  • @nomadic_shadow
    @nomadic_shadow 8 дней назад +1

    Thanks, Gary. I'm working on my C chops so this is a greatly timed video for me.

  • @Psychlist1972
    @Psychlist1972 8 дней назад +1

    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 9 дней назад +3

    Great video as always Mr. Explains!

  • @zrodger2296
    @zrodger2296 8 дней назад

    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  8 дней назад +1

      Yes, the Linux kernel coding standard forbids typedefs for structs as it says the typedef adds a layer of obscurity.

  • @superangrybrit
    @superangrybrit 9 дней назад +1

    Very good video! 🙂

  • @nathanaelsmith3553
    @nathanaelsmith3553 9 дней назад

    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.

  • @4115steve
    @4115steve 8 дней назад

    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  8 дней назад +1

      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.

  • @jecelassumpcaojr890
    @jecelassumpcaojr890 8 дней назад +1

    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 8 дней назад

      I still prefer a memcpy(), but I'm old fashioned! 😊

    • @GaryExplains
      @GaryExplains  8 дней назад

      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. "

  • @CanadianBakin42O
    @CanadianBakin42O 9 дней назад

    53 views 6 minutes ago Garry Explain fell off
    Also FIRST!

    • @GaryExplains
      @GaryExplains  9 дней назад

      Fell off?

    • @CanadianBakin42O
      @CanadianBakin42O 9 дней назад

      @@GaryExplains lol

    • @rap-soldier2185
      @rap-soldier2185 9 дней назад

      GOOD TEQUILA ​@@CanadianBakin42O

    • @4115steve
      @4115steve 8 дней назад

      @@GaryExplains it's referring to the viewing statistics on the video player, when views go down they "fell off". typical slang for children who can't explain things they mean.

    • @CanadianBakin42O
      @CanadianBakin42O 8 дней назад

      @@4115steve I'm not a fucking child, kid.