introduce nob_da_declare macro for simplifing dynamic array declaration#82
introduce nob_da_declare macro for simplifing dynamic array declaration#82abdorayden wants to merge 1 commit intotsoding:mainfrom
Conversation
There was a problem hiding this comment.
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);There was a problem hiding this comment.
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);
}|
Closed due to "No changes to public API policy" |
I noticed that you have to define a new structure even if you use it only once. nob_da_declare allows you to define a generic structure type in a single line instead of explicitly declaring a structure.