1

in this code i want to pass value of variable char array 'pass' in object "MyObject". But i am getting this error.

    Arduino: 1.6.11 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\XXX\Desktop\EEPROM_put\EEPROM_put.ino: In function 'void setup()':

EEPROM_put:17: error: array must be initialized with a brace-enclosed initializer

};

^

exit status 1
array must be initialized with a brace-enclosed initializer

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

when i change pass to *pass, result of EEPROM.get() is 1 and 6, instead of 1, 62345, 7539514682.

#include <EEPROM.h>
struct MyObject {
boolean stat;
char password[6];
char mobile[11];
}; 

char pass[6]="62345";
void setup() {
Serial.begin(9600);
while (!Serial);
MyObject customVar = {
  1,
  pass,
  "7539514682"
 };
EEPROM.put(1, customVar);
Serial.print("Written custom data type!");
}

void loop() {

}
2

Use class and constructor

#include <EEPROM.h>
class MyObject {
    boolean stat;
    char password[6];
    char mobile[11];
  public:
    MyObject(bool tempStat, char tempPassword[], char tempMobile[])
    {
      stat = tempStat;
      strcpy(password, tempPassword);//As suggested by mikael
      strcpy(mobile, tempMobile);
    }
};

char pass[6] = "62345";
void setup() {
  Serial.begin(9600);
  while (!Serial);
  MyObject customVar(true, pass, "7539514682");
  EEPROM.put(1, customVar);
  Serial.print("Written custom data type!");
}
| improve this answer | |
  • getting output like this: Read custom object from EEPROM: 1 62345 7539514682T 7539514682T – Tanmay Yerunkar Mar 19 '17 at 16:54
  • What is the expected Output – Sniper Mar 19 '17 at 17:25
  • Should copy with the sizeof(password) and not break on zero? strcpy() might be easier. – Mikael Patel Mar 19 '17 at 17:31
  • Sorry forgot NULL terminating character. Made changes – Sniper Mar 19 '17 at 17:35
  • @Sniper still not getting correct output, now getting some random characters, output is : 1 62ÿ! 75 75 Expected output is 1, 62345, 7539514682 – Tanmay Yerunkar Mar 19 '17 at 18:14
0

While the method from Sniper's answer works, it involves copying the data, which is something you often want to avoid on an Arduino. The simplest fix to your problem is probably to initialize your password field with a literal string, just like you did with the mobile field:

MyObject customVar = {
    1,
    "62345",
    "7539514682"
};

or maybe, if you want the password to be written near the top of the program,

#define PASS "62345"

MyObject customVar = {
    1,
    PASS,
    "7539514682"
};

Another option is to not store the strings in the object. Instead, store them elsewhere and have the object only hold pointers to them:

struct MyObject {
    boolean stat;
    const char *password;
    const char *mobile;
};

char pass[6] = "62345";

[...]

MyObject customVar = {
    1,
    pass,
    "7539514682"
};

Note that, in this case, the mobile number is stored by the compiler as an unnamed string constant. Because it is constant, the mobile field has been typed as const char *. I typed the password field identically for symmetry, but in this use case it need not be const.

| improve this answer | |

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.