0

I am trying to write a function to store the date and time data from an RTC module. I have used char arrays to implement the function. I get "declaration of 'varDay' as array of references" error upon compilation.

I am still learning about char arrays and manipulating them so may be I am making a very obvious mistake. Could you please explain what I am doing wrong?

#include <RTClib.h>

RTC_DS3231 rtc;

void setup() {
  Serial.begin(115200);
#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

}

void loop() {
  unsigned long newTime = millis();
  static unsigned long oldTime = 0;

  char varYear [] = "1234";
  char varMonth [] = "00";
  char varDay [] = "00";
  char varHour [] = "00";
  char varMinute [] = "00";
  char varSecond [] = "00";
  unsigned int varEpoch;
  char varDate [] = "123456789";
  char varTime [] = "12345678";
  char dash [] = "-";
  char colon [] = ":";


  if (newTime - oldTime >= 60000) {
    oldTime = newTime;

    getDate(varDay, varMonth, varYear, varDate, dash, colon);
  }

}
void getDate (char &varDay[3], char &varMonth[3], char &varYear[5], char &varDate[11], char &dash[2], char &colon[2]) {

  DateTime now = rtc.now();
  strcpy (varDay, now.day());
  strcpy (varMonth, now.month());
  strcpy (varYear, now.year());

  strcat (varDate, varDay) ;
  strcat (varDate, dash);
  strcat (varDate, varMonth);
  strcat (varDate, dash);
  strcat (varDate, varYear)

  Serial.println(varDate);
}

1 Answer 1

1

char &varDay[3] is an array of 3 references to type char, not a reference to type char of size 3.

Instead just pass a pointer:

void getDate (char *varDay, char *varMonth, char *varYear, char *varDate, char *dash[2], char *colon[2]) {

Also:

strcpy (varDay, now.day());
strcpy (varMonth, now.month());
strcpy (varYear, now.year());

That won't work. You can't copy a number into a string. now.day() etc need converting to a string first.

It would be better to format your string with snprintf:

snprintf(varDate, 11, "%02d-%02d-%04d", now.day(), now.month(), now.year());
1
  • Thanks a lot. I read your post "Evils of Arduino Strings" so was trying to implement this without using String class. So it was my first attempt dealing with char arrays. I didn't know about snprintf function so learnt something new :) still a lot more to learn though Commented Aug 12, 2020 at 20:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.