How can I add elements to a non-fixed size array in C++?
First of all, the size of the array is fixed. You can just skip specifying the size when you initialize an array like this,
int cells[] = {0};
The size of the array is inferred automatically by the compiler.
If you want dynamic size arrays, use vector. See the code below for an example,
#include <iostream>
#include <vector>
int main(){
int cells1[] = {0}, cells2[] = {0,1,2,3,4,5};
std::cout<<"Size of cells1 array: "<<sizeof(cells1)/sizeof(cells1[0])<<std::endl; // 1
std::cout<<"Size of cells2 array: "<<sizeof(cells2)/sizeof(cells2[0])<<std::endl; // 6
std::vector<int> cells3{0}; // Vector - a dynamic array
std::cout<<"Size of cells3 vector: "<<cells3.size()<<std::endl; // 1
cells3.push_back(1); // Append an element to the end of vector
std::cout<<"Size of cells3 vector: "<<cells3.size()<<std::endl; // 2
return 0;
}
Output
Size of cells1 array: 1
Size of cells2 array: 6
Size of cells3 vector: 1
Size of cells3 vector: 2