Whilst revisiting the use of ‘C’ I remembered about ‘unions’ and how they can be used to view data in memory in different ways.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

And here is the code:

#include <stdio.h>

union {
    unsigned int i;
    unsigned char c;
} aunion;

int main() {
    aunion.i = 42;

    printf("The answers are %d and %c\nWhy is that?\n", aunion.i, aunion.c);
    printf("Unions are a facinating way of manipulating memory content\n");
    printf("But can be dangerous, which is why you will not find them in Java\n");
    printf("What is happening with the union 'aunion'?\n\n");

    printf("And like the last video, this is fast to compile\n");
    printf("But with Java you can take the .class file and run it on anything\n");
    printf("with a a Java Virtual Machine.\n");

    return aunion.i;
}