Created
February 27, 2024 07:36
-
-
Save rexim/b5b0c38f53157037923e7cdd77ce685d to your computer and use it in GitHub Desktop.
Revisions
-
rexim created this gist
Feb 27, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ #include <stdio.h> #include <stdlib.h> #define da_append(xs, x) \ do { \ if ((xs)->count >= (xs)->capacity) { \ if ((xs)->capacity == 0) (xs)->capacity = 256; \ else (xs)->capacity *= 2; \ (xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \ } \ \ (xs)->items[(xs)->count++] = (x); \ } while (0) typedef struct { int *items; size_t count; size_t capacity; } Numbers; int main(void) { Numbers xs = {0}; for (int x = 0; x < 10; ++x) da_append(&xs, x); for (size_t i = 0; i < xs.count; ++i) printf("%d\n", xs.items[i]); return 0; }