How to add strings to an array

Hi,
I need to create an array of strings to hold tag data but i cant quite figure out how to do it (i am relatively new to arduino and C programming).

The code below is what i have done so far (this code reads lines (tag) from a file - if the tag is different it adds it to the array). Any help with this is much appreciated.

Cheers.

void distinctEntries() {
  dataFile = SD.open(lookFor); //open file for read
  tag = String();
  prevTag = String();
  char* tagStrings[] = {};
  int i = 0;  
  
  if (dataFile) {
    while (dataFile.available()) {
      char ch = dataFile.read();
      
      while (ch != '\n') {
        tag += ch;
        ch = dataFile.read();       
      }
      
      tag = tag.substring(9);    //dont need the first 9 characters
      
      if (tag != prevTag) {         //this is also spose to check the other entries in the array but not implemented yet
        strcpy(tagStrings[i],tag);    //or something that actually works
        i++;
      }
      
      prevTag = tag;
      tag = String();   
    }
    
    // close the file:
    dataFile.close();
  } 
  else {
  	// if the file didn't open, print an error:
    Serial.println("error opening file");
  }
  
}

Well, your array of strings is actually an array of character pointers. Which is fine. The problem is that there actually needs to be some arrays of characters to store the data. This is either done by allocating space dynamically at run time, or by declaring and reserving space statically at compile time.

If all the strings are the same size, or if you know what size they are going to be, or if they're close to the same size, it's usually more straightforward to declare the space at compile time.

#define MAX_TAGS 20

char tagStrings[MAX_TAGS][17];  // Reserve space for 20 tags of max size 16 + null
int tagCount = 0;
...
  strcpy(tagStrings[tagCount], tag);
  ++tagCount;
  if (tagCount >= MAX_TAGS)
    barf("Too many tags");    // Or something

Normally in C, dynamically allocated space is done with malloc().

#define MAX_TAGS 20
char *tagStrings[MAX_TAGS];
int tagCount = 0;
...
  tagStrings[tagCount] = malloc(strlen(tag) + 1);  // + 1 for the null terminator
  if (tagStrings[tagCount] != NULL)  // malloc() returns a NULL if you're out of memory
  {
    strcpy(tagStrings[tagCount], tag);
    ++tagCount;
...
  }
  else
  {
    barf("Out of memory");  // or something
  }

Well, your array of strings is actually an array of character pointers.

But how big is the array?

Which is fine.

Not the way that OP's code is written.

Copying a String to a non-existant pointer (of a different type) pointing to nowhere is not a good idea.

OP. The first thing that you need to decide is whether you want to use strings (NULL terminated array of char) or Strings. You can not copy a String to a string using strcpy().

Then, you need to make sure that you actually have an array of pointers of the correct size and type.

Finally, you need to make sure that each element in that array (a pointer) actually points to existing space where the pointed to data can be copied.

Hi TanHadron,
Thanks for the reply. I decided to go with the static approach as it seems simpler and easier to understand however when i used your code example i got the following error (error: cannot convert ‘String’ to ‘const char*’ for argument ‘2’ to ‘char* strcpy(char*, const char*) for the line strcpy(tagStrings[tagCount], tag);

Do i need to first convert the string to an array of char?

Any help is much appreciated.

Cheers.

Do i need to first convert the string to an array of char?

A string IS an array of char. A String is not. You need to ask your question correctly.

PaulS is correct. I totally used the wrong copy function.
I'm still not sure this is the right way to accomplish what you want, but I think this syntax will work:

//  strcpy(tagStrings[tagCount], tag);          // Not this  
    tag.toCharArray(tagStrings[TagCount], 17);  // This

I need to create an array of strings to hold tag data but i cant quite figure out how to do it (i am relatively new to arduino and C programming).

If you are using the String functions, the below page has info that probably supports what you want to do without getting into C string, c-string, or "string".