This question already has answers here:
Vectors in Arduino
(7 answers)
Closed 9 years ago.
I have the following variables:
char* words[20];
char* tracks[20];
where they represent the number of tracks and words saved in a database on a SD card. My question is that, I would like the array of words and tracks to be of variable size and not static, that it is I don't want to specify the size of the array.
I suggest you take a look at Standard C++ for Arduino It lets you use STL (standard template library) vector
Related
This question already has answers here:
how to set or initialize default value for all elements of a table or 2d array or multdimentional array
(4 answers)
Closed 3 years ago.
I have two dimensional char array in c++ and want to give all element one value without using loops
is there any function to achieve it ?
char demo[5][7];
memset(demo, '\0', sizeof(demo));
Based on the provided info, does this do what you want?
Since you're using C++ I'd question to use a native char array, as long as you're not required to and/or needing to use the stack.
Otherwise its idiomatic to use the collection classes provided by the STL.
Adding to the answer as requested, this obviously does what the question asks for: initializing the memory of a char array.
Please see the docs for more information on memset: https://en.cppreference.com/w/cpp/string/byte/memset
This question already has answers here:
How can I split a string by a delimiter into an array?
(13 answers)
Closed 3 years ago.
I want to get an unknown number of integers separated by space as user input. How can I store them in an array?
You could keep changing the size of the array (since you don't know how big to make it). Or use a data structure (like a vector) designed to change size.
This question already has answers here:
How do I find the length of an array?
(30 answers)
Closed 6 years ago.
I have a function like:
function(double* numbers)
I know that if I know the size of the numbers array, I can then do
double* numbers = new double[size];
function(numbers)
This will return the value in numbers correctly, however, now I don't know the size of numbers, I still want to use function function, which is a third party library function which I don't want to change, is there a way to get the array out without knowing the size of array?
No, there's no standard way to retrieve the size of the array at run time from a mere pointer. I suggest you either use std::array for fixed-size arrays, or std::vector for variable-length arrays. An less safe alternative would be to pass the size of the array together with the pointer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
View array in Visual Studio debugger?
I declare an array of 50 elements using:
int *ans;
ans = new int[50];
yet when I'm looking at the 'Locals' list and try to expand the array, it have only one
element, why's that? am I doing something wrong?
The locals list just knows you have a pointer to an integer. It doesn't have a good way to know how big that array is, unfortunately. It could probably be made more sophisticated, or maybe you can create your own custom viewer for that variable - it depends on what IDE you're using probably.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
combination and permutation in C++
I have a vector of say size "n". Lets say for example a vector of n=4, <1,2,3,4>. How can I generate all n-1 combinations of this vector. In this example, 4 chose 3. I want the output to be <1,2,3> <1,2,4> <1,3,4> <2,3,4>. Thanks.
Start by looking up STL's next_permutation function.