Skip to content

Instantly share code, notes, and snippets.

@mbernson
Created December 14, 2022 12:19
Show Gist options
  • Save mbernson/f6c6120fca10c413d647e20d2e399fe4 to your computer and use it in GitHub Desktop.
Save mbernson/f6c6120fca10c413d647e20d2e399fe4 to your computer and use it in GitHub Desktop.
C array on the heap
#include <stdio.h>
#include <stdlib.h>
typedef struct Point {
int x;
int y;
} Point;
int main() {
int count = 2;
Point* points = malloc(sizeof(Point) * count);
points[0] = (Point) { 1, 2 };
points[1] = (Point) { 3, 4 };
printf("Hello World!\n");
for (int i = 0; i < count; i++) {
Point p = points[i];
printf("Point %d: %d, %d\n", i, p.x, p.y);
}
free(points);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment