Pages: [1]   Go Down
Author Topic: AGHW-Arduino-Pass C++ string to function as parameter...  (Read 792 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have the function:
Code:
// Open the EDB table, if existed or created...
void AG_Table_Open(unsigned long APtr_AGHead, char* AID_AGHead){
  DBGL3_PRN("(1) AID_AGHead: ");
  DBGL3_PRN(AID_AGHead);
  DBGL3_PRN(", size: ");
  DBGL3_PRNLN(sizeof(AID_AGHead));

  AG_LogStruct strucRec;
  String dbDescr;
/*  DBGL3_PRN("(");
  DBGL3_PRN(AID_AGHead);
  DBGL3_PRN("=");
  DBGL3_PRN(AG_EDBIndex_ID);
  DBGL3_PRNLN(")=========================");*/

  if (strcmp(AID_AGHead, AG_EDBIndex_ID)==0){
//  if (AID_AGHead==AG_EDBIndex_ID){
    dbDescr = "AG_EDBIndex";
  }
  else {
    LOG_PRN("unknown AG_Table! (");
    LOG_PRN(AID_AGHead);
    LOG_PRNLN(")");
    return;
  }
  EDB db(&AG_Table_EEPROM_Writer, &AG_Table_EEPROM_Reader);
  DBGL3_PRNLN("DB object created...");
  int memPos = APtr_AGHead+sizeof(AID_AGHead)-1;

  if (EEPROM.read(APtr_AGHead + 0) == AID_AGHead[0] &&
      EEPROM.read(APtr_AGHead + 1) == AID_AGHead[1] &&
      EEPROM.read(APtr_AGHead + 2) == AID_AGHead[2]) {
    DBGL3_PRN(dbDescr);
    DBGL3_PRN(" DB exists... (mem loc: ");
    DBGL3_PRN(memPos, DEC);
    DBGL3_PRNLN(")");
    if (db.open(memPos)==EDB_OK){
      DBGL3_PRN(dbDescr);
      DBGL3_PRN(" DB opened... (RecCount: ");
      DBGL3_PRN(db.count(), DEC);
      DBGL3_PRNLN(")");
    }
    else {
      DBGL3_PRN(dbDescr);
      DBGL3_PRNLN(" DB could not opened...!");
//    db.create(200, TABLE_SIZE, sizeof(logUpPeriod));
//    db.create(300, 100, sizeof(logUpPeriod));
//    DBGL3_PRNLN("DB created...");
    }
  }
  else {
    DBGL3_PRN("AID_AGHead: ");
    DBGL3_PRN(AID_AGHead);
    DBGL3_PRN(", size: ");
    DBGL3_PRNLN(sizeof(AID_AGHead));
    for (unsigned int t=0; t<sizeof(AID_AGHead); t++)
      EEPROM.write(APtr_AGHead + t, *((char*)&AID_AGHead + t));
    DBGL3_PRN(dbDescr);
    DBGL3_PRNLN(" DB header ID saved...!");
    if (strcmp(AID_AGHead, AG_EDBIndex_ID)==0){
//    if (AID_AGHead==AG_EDBIndex_ID){
      db.create(memPos, 120, sizeof(strucRec));
    }
  }
}

I am calling it through:
Code:
 AG_Table_Open(AG_EDBIndex_START, AG_EDBIndex_ID);

where:
Code:
#define AG_EDBIndex_START 50
#define AG_EDBIndex_ID "a@i"

The sizeof AID_AGHead (inside the function) is 2! (instead of 4) but the contents looks OK ("a@i")...!?

Why is this happening?

What I am doing wrong?

What is the correct way of passing strings in Arduino (C++)?

Thank you
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What you are passing to the function is a pointer. What you are getting the size of is a pointer. Pointers are ALWAYS the same size. There is no way, given a pointer to an array, to determine the size of the array pointed to.

For character arrays, there is a convention to NULL terminate the array. The strlen() function will then tell you how much of the array contains data that might be interesting, but it won't tell you how many characters the array CAN hold.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I get the char array (through the pointer)? so the sizeof will be working...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can I get the char array (through the pointer)? so the sizeof will be working...
Was there some part of "you can't" that you didn't understand?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    for (unsigned int t=0; t<sizeof(AID_AGHead); t++)
      EEPROM.write(APtr_AGHead + t, *((char*)&AID_AGHead + t));

In this part of the function I will change the sizeof with the strlen()... but I have problem also with the bytes which get write in the EEPROM... It seems that *((char*)&AID_AGHead + t) does not return the correct byte (from the AID_AGHead)...

Do I have to change it to *((char*)AID_AGHead + t) ? (I cannot test it right now)... in order to work?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In this part of the function I will change the sizeof with the strlen()
I wouldn't do that. You really need to add a 3rd argument, to pass in the size of the array. Each record being written to EEPROM should be the same size, so each starts some known distance from the root. If you use strlen(), each record will be a different size.

Quote
It seems that *((char*)&AID_AGHead + t) does not return the correct byte (from the AID_AGHead)...
What do you mean "does not return the correct byte"? The EEPROM.write() function does not return the bytes written.

Logged

Pages: [1]   Go Up
Jump to: