How do I add something at the end of a C ++ array?
3 answers
From the comments, it sounds like you don't want to add to the end of the array, but partially fill the array and keep track of how much data you've written. You just need a variable to keep track of this:
char array[10];
size_t size = 0;
// Add characters:
array[size++] = 'H';
array[size++] = 'e';
array[size++] = 'l';
array[size++] = 'l';
array[size++] = 'o';
You need to make sure that you never go outside the array, otherwise you will damage other memory.
+10
source to share
You can create another array that is larger than the first and copy all the elements, then add the new element to the end of the array.
alternatively you can convert the array to a vector, add an element, and then convert the vector to an array back. Take a look at: How to convert a vector to an array in C ++ , What is the simplest way to convert an array to a vector? p>
+1
source to share