Created
December 14, 2022 12:19
-
-
Save mbernson/f6c6120fca10c413d647e20d2e399fe4 to your computer and use it in GitHub Desktop.
C array on the heap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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