Created
December 23, 2025 04:12
-
-
Save riturajborpujari/c9b0a762617b3c58c13f832634b9c243 to your computer and use it in GitHub Desktop.
Dynamic Array in C
This file contains hidden or 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
| #ifndef __D_ARRAY_H__ | |
| #define __D_ARRAY_H__ | |
| #include <stdlib.h> | |
| #define da_init(name, type) \ | |
| typedef struct {\ | |
| type *items;\ | |
| size_t count;\ | |
| size_t capacity;\ | |
| } name;\ | |
| #define da_append(xs, x) \ | |
| do {\ | |
| if (xs.capacity == xs.count) {\ | |
| if (xs.capacity == 0) xs.capacity = 128;\ | |
| else xs.capacity *= 2;\ | |
| xs.items = realloc(xs.items, xs.capacity * sizeof(xs.items));\ | |
| }\ | |
| xs.items[xs.count++] = x;\ | |
| } while (0); | |
| #define da_free(xs) \ | |
| do {\ | |
| free(xs.items);\ | |
| } while(0); | |
| #endif /* __D_ARRAY_H__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment