Skip to content

Instantly share code, notes, and snippets.

@emandret
Created March 25, 2023 19:17
Show Gist options
  • Select an option

  • Save emandret/c36331ba4afde0caee9f9d7da43a4237 to your computer and use it in GitHub Desktop.

Select an option

Save emandret/c36331ba4afde0caee9f9d7da43a4237 to your computer and use it in GitHub Desktop.

Revisions

  1. emandret created this gist Mar 25, 2023.
    32 changes: 32 additions & 0 deletions struct_memory.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #include <stdio.h>

    #pragma pack(1)
    struct my_struct
    {
    unsigned char b0;
    unsigned char b1;
    unsigned char b2;
    unsigned char b3;
    unsigned char b4;
    };

    int main(void)
    {
    struct my_struct *ptr;
    unsigned char buffer[5];

    ptr = (struct my_struct *) buffer;

    ptr->b0 = 'h';
    ptr->b1 = 'e';
    ptr->b2 = 'l';
    ptr->b3 = 'l';
    ptr->b4 = 'o';

    for (int i = 0; i < 5; i++)
    {
    putchar(buffer[i]); // Print "hello"
    }

    return 0;
    }