How to copy a char array to a another char array

I am trying to copy a char array to another array but it did not worked. Inside this myTag array I am going to store the RFID tag numbers. The myTags array is saved in the EEPROM. I want to store a newly read RFID tag number which is not in the myTag array in to the EEPROM.
for that I tried creating another char array named char_array to store the converted tag number which will be read by the arduino as a string. Then I tried to copy that array value to the main array myTags and then save it in the EEPROM. But this was not successful. Can anyone help me on this.

boolean granted = false;
char myTags[][9] = {"37376B34"};
char reloadedTags[sizeof(myTags) / sizeof(myTags[0])][9];

char char_array[9];

This is the code for read from the EEPROM and checking with the read tag number.

void checkAccess (String StrUID) {            //Function to check if an identified tag is registered to allow access

  while (!Serial);
  EEPROM.get(0, reloadedTags);

  for (int tag = 0; tag < (sizeof(myTags) / sizeof(myTags[0])); tag++) {
    for (int c = 0; c < 9; c++) {
      temp += reloadedTags[tag][c];
    }
    if (temp == StrUID) {
      open();
      granted = true;
    }
    else{
      granted = false;
      }
    temp = "";
  }
 return granted;
}

This is the code that I tried to copy the arrays.

void add(String StrUID) {

  if(granted==false){

  StrUID.toCharArray(char_array, 9);
  //Serial.print(char_array);
  
  //memcpy(myTags, char_array, (sizeof(myTags) / sizeof(myTags[0])));
  myTags[(sizeof(myTags) / sizeof(myTags[0]))][9] = char_array;
  //Serial.println((sizeof(myTags) / sizeof(myTags[0])));
   while (!Serial);
  EEPROM.update(0, myTags);

}
}

Why are you using the String class?

Why are you mixing strings and Strings?
Stick with just one - strings.

...then you can use strcpy() to copy the string.

Can you please explain me that thing. I did not get it

An example

char string1[] = {"data in string1"};
char string2[20] = {};  //plenty of room for the source string plus termination

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  strcpy(string2, string1);
  Serial.print("string2 now holds this : ");
  Serial.println(string2);
}

void loop()
{
}

UKHeliBob, in this code, it completely removes what is in the string2 array and copy the content from string1. in my case i want to just keep the data which is already in and to do just update with the new data. Is that possible to do.

UKHeliBob:
An example

char string1[] = {"data in string1"};

char string2[20] = {};  //plenty of room for the source string plus termination

void setup()
{
 Serial.begin(115200);
 while (!Serial);
 strcpy(string2, string1);
 Serial.print("string2 now holds this : ");
 Serial.println(string2);
}

void loop()
{
}

UKHeliBob, in this code, it completely removes what is in the string2 array and copy the content from string1. in my case i want to just keep the data which is already in and to do just update with the new data. Is that possible to do.

What is in your string2 ?
What is in your string1 ?
Which part of string1 should replace which part of string2 ?
Are the strings and data always the same length and in the same place in the strings ?

char string1[] = {"data in string1"};
char string2[20] = {"Xdata in string2X"};  //plenty of room for the source string plus termination

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.print("string2 starts like this : ");
  Serial.println(string2);
  strcpy(string2, "X");
  strcat(string2, string1);
  strcat(string2, "X");
  Serial.print("string2 now holds this : ");
  Serial.println(string2);
}

void loop()
{
}

UKHeliBob, my one array is as follows.

char myTags[][9] = {"37376B34","7AA29B1A","54A23C1F"};

I want to add the value of the following variable to the above array.

String StrUID;

For that I created a another char array and passed the above string value to it.

char char_array[9]; // this is the created char array
StrUID.toCharArray(char_array, 9);

Then I tried to add the value of that char array to the myTags array. In this all cases the length of the data is equal. but it didn't worked. is there a possible way to do this. I want to save that strUID value in the char array as a new data while keeping the already saved data.

You can't add; myTags is a fixed size at compile time.

You will need to allocate a fixed size, e.g. to store a maximum of 10 tags; the first 3 are predefined.

char myTags[10][9] = {"37376B34","7AA29B1A","54A23C1F"}:

Next you can replace e.g. the 4th element with a strcpy.

Here is a sketch that does that using SafeString library. Once you have wrapped the myTags[4] element in a SafeString you can just assign a string to it.

The sample output is

37376B34
7AA29B1A
54A23C1F







37376B34
7AA29B1A
54A23C1F

somemore

However if you stuff up and try to put to many chars into that element e.g.
myTags4 = "some more";
The SafeString library will catch the error and display an error msg, i.e.
Much more reliable then coding with strcpy.

Error: myTags4.concat() needs capacity of 9 for the first 9 chars of the input.
        Input arg was 'some more'
        myTags4 cap:8 len:0 ''
#include <SafeString.h>  // install SafeString from library manager
// see tutorial at https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

const size_t STR_LEN = 9;
char myTags[10][STR_LEN] = {"37376B34","7AA29B1A","54A23C1F"};

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) { // pause a little to give you time to open the Arduino monitor
    Serial.print(i); Serial.print(' '); delay(500);
  }
  Serial.println();
  SafeString::setOutput(Serial);
  
  // put your setup code here, to run once:
  for (int i=0; i<10; i++) {
    Serial.println(myTags[i]);
  }
  createSafeStringFromCharPtrWithSize(myTags4, myTags[4], STR_LEN);  // wrap the [4] element in a SafeString
  // OR cSFPS(myTags4, myTags[4], STR_LEN); for short
  myTags4 = "somemore"; 
    
  for (int i=0; i<10; i++) {
    Serial.println(myTags[i]);
  }
}

void loop() {
}

Thank you very much. I'll try this.

drmpf:
Here is a sketch that does that using SafeString library. Once you have wrapped the myTags[4] element in a SafeString you can just assign a string to it.

The sample output is

37376B34

7AA29B1A
54A23C1F

37376B34
7AA29B1A
54A23C1F

somemore




However if you stuff up and try to put to many chars into that element e.g.
myTags4 = "some more"; 
The SafeString library will catch the error and display an error msg, i.e.
Much more reliable then coding with strcpy.



Error: myTags4.concat() needs capacity of 9 for the first 9 chars of the input.
       Input arg was 'some more'
       myTags4 cap:8 len:0 ''










#include <SafeString.h>  // install SafeString from library manager
// see tutorial at The SafeString alternative to Arduino Strings for Beginners Safe, Robust, Debuggable replacement String class for Arduino

const size_t STR_LEN = 9;
char myTags[10][STR_LEN] = {"37376B34","7AA29B1A","54A23C1F"};

void setup() {
 Serial.begin(9600);
 for (int i = 10; i > 0; i--) { // pause a little to give you time to open the Arduino monitor
   Serial.print(i); Serial.print(' '); delay(500);
 }
 Serial.println();
 SafeString::setOutput(Serial);
 
 // put your setup code here, to run once:
 for (int i=0; i<10; i++) {
   Serial.println(myTags[i]);
 }
 createSafeStringFromCharPtrWithSize(myTags4, myTags[4], STR_LEN);  // wrap the [4] element in a SafeString
 // OR cSFPS(myTags4, myTags[4], STR_LEN); for short
 myTags4 = "somemore";
   
 for (int i=0; i<10; i++) {
   Serial.println(myTags[i]);
 }
}

void loop() {
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.