As stated by Sim Son in a comment, an array is a fixed-size data
structure in C++. You thus have to choose beforehand a suitable size,
and then keep track of how much of the array is actually used. For
example:
const int AB_max = 10; // array capacity
const char *AB_str[AB_max] = {"29 EC C7 C1", "69 5B C9 C2",
"22 3B 83 34", "12 BF BF 34", "C6 78 8E 2C"};
int AB_count = 5; // used slots
To add a string to this array, you write it to the next available slot.
But you have first to make sure there is still room available:
void AB_add(const char *s) {
if (AB_count < AB_max)
AB_str[AB_count++] = s;
}
If you want to remove an item (other than the last one), you will have
to shift all the following items down the array. The memmove()
function comes handy for this:
void AB_remove(const char *s) {
// Find the index of s.
int i;
for (i = 0; i < AB_count; i++)
if (strcmp(s, AB_str[i]) == 0) break;
// Do nothing if not found.
if (i == AB_count) return;
// Move subsequent items down the array.
memmove(&AB_str[i], &AB_str[i+1],
(AB_count - (i + 1)) * sizeof *AB_str);
AB_count--;
}
Note that moving these items down can be costly if the array is pretty
long. An array may then not be the best data structure for you. I
wouldn't worry though if it's just a dozen items or so.
Here is now a small test for all of the above:
void AB_print() {
Serial.print("AB: ");
Serial.print(AB_count);
Serial.println(" items");
for (int i = 0; i < AB_count; i++) {
Serial.print(" ");
Serial.println(AB_str[i]);
}
Serial.println();
}
void setup() {
Serial.begin(9600);
AB_print();
AB_add("00 00 00 00");
AB_print();
AB_remove("12 BF BF 34");
AB_print();
}
void loop() {}
Note that this is a very “C” style of doing things. If you want to go to
more idiomatic C++, you can start by putting everything that is named
with the prefix AB_
inside a class.
If you really want an array where you can vary the length, you may
allocate it with malloc()
and resize it with realloc()
. These
functions, however, are not very friendly to memory-constrained devices.
They are thus often avoided in the embedded world. They may also not
play well with code that uses new
and delete
, including library code
that you may not be aware of.
sizeof(*StrAB)
is not the length of the string, nor the length of the array (that's why you apparently tried to fix it by adding 1). The type of*StrAB
ischar[]
, which is equivalent tochar*
, sosizeof(*strAB)
returns the same assizeof(void*)
.