eteran / c-vector

A dynamic array implementation in C similar to the one found in standard C++
MIT License
737 stars 109 forks source link

Add macro cvector_for_each_in #36

Closed julianhartmer closed 2 years ago

julianhartmer commented 2 years ago

Add for loop header macro cvector_for_each_in to mimic frequently used range-based for cvectors. With this macro, it might be a good idea to remove cvector_for_each and cvector_free_each_and_free, because they break C89 compatibility. Declaring a variable is not permitted in C89. It might be a good idea to merge cvector_for_each_in if the other macros are removed, because cvector_utils.h only contains one macro.

Example usage of cvector_for_each_in:

int *it;
cvector_vector_type(int) v = NULL;

cvector_push_back(v, 10);
cvector_push_back(v, 12);

cvector_for_each_in(it, v) {
    printf("%d\n", *it);
}

The example prints out all members of cvector v.

eteran commented 2 years ago

A no brainer, this is a good PR 👍🏻 Thanks!