0

Here is my code:

char *StrAB[] = {"29 EC C7 C1", "69 5B C9 C2", "22 3B 83 34",  "12 BF BF 34", "C6 78 8E 2C" };  
void setup() {  
  Serial.begin(9600);  
}  
void loop() {  
  for (int i = 0; i < sizeof(*StrAB)+1; i++)   
  {  
    Serial.println(StrAB[i]);  
    delay(1000);  
  }  
  Serial.println(sizeof(*StrAB)+1);
  StrAB[sizeof(*StrAB)+1]= "00 00 00 00";
}  

I'm trying to append to the end of array of strings but it doesn't work? How can I delete a string from the array?

6
  • 2
    You can't append to an array as they have a fix size. You need to know how many entries are expectable at max and define the array's size accordingly. You also better use char arrays instead of your Strings, they appear to have a fixed size anyway.
    – Sim Son
    May 23 at 12:28
  • Tnx Sim. do you how can I delete a string from the array ?
    – dave
    May 23 at 12:43
  • All you can do is to assign an empty string to an areay element. It not possible to append/add/remove an element of a C array, you can only change an element's value.
    – Sim Son
    May 23 at 12:45
  • thank you so much that was so helpful.
    – dave
    May 23 at 13:20
  • 2
    Note that 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 is char[], which is equivalent to char*, so sizeof(*strAB) returns the same as sizeof(void*).
    – PMF
    May 23 at 16:45

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.