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

[Question] example of vector of chars #10

Closed Hillard28 closed 3 years ago

Hillard28 commented 3 years ago

Do you happen to have an example of a c-vector of chars? Having a bit of trouble adapting the example and would be helpful to see that implementation!

I assumed chars are supported but is it just numerics?

eteran commented 3 years ago

It should support any type!

I haven't tested it, but just off the top of my head, I'd expect this to work:

// declare it as a vector of chars
cvector_vector_type(char) v = NULL;

// populate it
cvector_push_back(v, 'h');
cvector_push_back(v, 'i');
cvector_push_back(v, '!');
cvector_push_back(v, '\0');

// use it
printf("%s\n", v);

// free it
cvector_free(v);

Note that it won't automatically put the \0 terminator in there for you since this is just storage, so you'll have to manage that on your own if you intend to use this for c-string APIs (as shown above)

eteran commented 3 years ago

Closing because I believe this answers your question. But please re-open if you run into any issues.