Just wondering how I could go about appending to an array. In python you just use .append() but I was wondering if there is a similar function in Arduino?
Just make the array big enough to hold all the elements you expect in the first place.
So if I wanted to add to that array later in the program would I just say += and then the value I want to add? Will that work the same as in python?
If you want to add to something, use a list.
Memory management isn't a microcontroller with only 2k of RAM's forte.
so do I just use += ??
No.
you didn't even answer my question here either though??
Find a C++ tutorial site.
It'll save us all time.
what's the point of this forum then? It's called "Programming Questions" - I've been researching that question for the past few days and have been unsuccessful. That's why I came on here as a "last resort" to see if someone would be kind enough to help me - but to no avail. I wouldn't be asking these questions on here if I didn't already search for them tirelessly. So I ask, can you answer my questions please
In C/C++, an array is a fixed sized, so no, you can't append to it.
Use a list.
Thank you
C / C++ come with bare metal and not much else.
Any abstract datatypes you have to build yourself, or get from a library. Note that on a microcontroller with
2k of RAM in total many conveniences of higher-level programming are not really available.
In C you'd typically use an array and indexes into the array to implement a rudimentary list. C does not
protect you at all from subscripting outside an array, note.
Thus
#define SIZE 20
int list[20]; // the array
byte list_end = 0 ; // the end pointer
// A function to append - it may fail if the array is full, but returns a boolean to indicate if it worked.
bool append_list (int element)
{
if (list_end < SIZE)
{
list[list_end++] = element ;
return true;
}
return false;
}
Thank you very much MarkT !!