Skip to content

Instantly share code, notes, and snippets.

@avar

avar/coord.c

Created Dec 6, 2018
Embed
What would you like to do?
Shows that (in practice, not standard) structs are just a fancy linear container of their types (assuming alignment works out) in memory
#include <stdio.h>
#include <stdlib.h>
struct coord {
int x;
int y;
int z;
};
int main(void)
{
struct coord *v = malloc(sizeof(struct coord) * 2);
int *ptr = (int*)v;
for (int i = 0; i < 6; i++)
ptr[i] = 100 + i;
for (int i = 0; i < 2; i++)
printf("v[%d] = {%d, %d, %d}\n", i, v[i].x, v[i].y, v[i].z);
return 0;
}
@avar

This comment has been minimized.

Copy link
Owner Author

@avar avar commented Dec 6, 2018

$ gcc -Wall -Wextra -Wpedantic -o test test.c; ./test
v[0] = {100, 101, 102}
v[1] = {103, 104, 105}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment