Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions nob.h
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used practically the same macro for some time and find it neat.
As you describe here it can be used as sort of "anonymous" type. However this usage might make some compilers warn about argument types. But it works i guess.

nob_declare(int) name = {0};

void fu(nob_declare(int) * inp){
    return;
}

fu(&name);

You can typedef which solves the warnings.

 typedef nob_declare(float) Some;

 Some name = {0}; 

 void fu(Some * inp){
     return;
 }

fu(&name);

Copy link
Author

@abdorayden abdorayden Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for sharing your experience with nob_da_declare and the suggestion about using typedef to avoid warnings.

You're absolutely right-this macro effectively creates an anonymous structure, and using typedef helps maintain cleaner code and prevents compiler warnings.

And you have ability to declare and use these structures globally (outside main) is a great point -it adds flexibility in how the macro can be applied

// Declared as global variable 
nob_da_declare(int) numbers = {0};

void foo(){
    nob_da_append(&numbers , 10);
}

Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,27 @@ NOBDEF bool nob_delete_file(const char *path);
(da)->items[j] = (da)->items[--(da)->count]; \
} while(0)

// Declare generic structure for simplify declaration dynamic array. Example:
// ```c
//
// nob_declare(int) xs = {0};
// nob_da_append(&xs, 69);
// nob_da_append(&xs, 420);
// nob_da_append(&xs, 1337);
//
// nob_declare(char) chrs = {0};
// nob_da_append(&chrs, 'a');
// nob_da_append(&chrs, 'b');
// nob_da_append(&chrs, 'c');
//
// ```
#define nob_da_declare(type) \
struct { \
type *items; \
size_t capacity; \
size_t count; \
}

// Foreach over Dynamic Arrays. Example:
// ```c
// typedef struct {
Expand Down Expand Up @@ -2005,6 +2026,7 @@ NOBDEF int closedir(DIR *dirp)
#define delete_file nob_delete_file
#define return_defer nob_return_defer
#define da_append nob_da_append
#define da_declare nob_da_declare
#define da_free nob_da_free
#define da_append_many nob_da_append_many
#define da_resize nob_da_resize
Expand Down
12 changes: 12 additions & 0 deletions tests/da_declare.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#define NOB_DA_INIT_CAP 4
#include "nob.h"

int main(void){
da_declare(int) xs;
da_append(&xs , 10);

nob_log(INFO , "Hello World %d " , xs.items[0]);
return 0;
}