What does this ampersand mean?

I'm studying the example datecalc.ino from the RTClib library
https://github.com/adafruit/RTClib

I'm stumped almost at once by this unfamiliar line:

void showDate(const char* txt, const DateTime& dt)

I understand the asterisk to be a 'pointer'. But what does the second parameter mean? IOW, how should I 'read' it? Preferably in 'Arduino programmer' terms?

From research so far I suspect that may be a big ask. If so I'll just have to accept it's C++ stuff, and copy/paste on trust to proceed with my project.

Here's the full code:

// Simple date conversions and calculations

#include "RTClib.h"

void showDate(const char* txt, const DateTime& dt) {
    Serial.print(txt);
    Serial.print(' ');
    Serial.print(dt.year(), DEC);
    Serial.print('/');
    Serial.print(dt.month(), DEC);
    Serial.print('/');
    Serial.print(dt.day(), DEC);
    Serial.print(' ');
    Serial.print(dt.hour(), DEC);
    Serial.print(':');
    Serial.print(dt.minute(), DEC);
    Serial.print(':');
    Serial.print(dt.second(), DEC);

    Serial.print(" = ");
    Serial.print(dt.unixtime());
    Serial.print("s / ");
    Serial.print(dt.unixtime() / 86400L);
    Serial.print("d since 1970");

    Serial.println();
}

void showTimeSpan(const char* txt, const TimeSpan& ts) {
    Serial.print(txt);
    Serial.print(" ");
    Serial.print(ts.days(), DEC);
    Serial.print(" days ");
    Serial.print(ts.hours(), DEC);
    Serial.print(" hours ");
    Serial.print(ts.minutes(), DEC);
    Serial.print(" minutes ");
    Serial.print(ts.seconds(), DEC);
    Serial.print(" seconds (");
    Serial.print(ts.totalseconds(), DEC);
    Serial.print(" total seconds)");
    Serial.println();
}

void setup () {
    Serial.begin(115200);

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

    DateTime dt0 (0, 1, 1, 0, 0, 0);
    showDate("dt0", dt0);

    DateTime dt1 (1, 1, 1, 0, 0, 0);
    showDate("dt1", dt1);

    DateTime dt2 (2009, 1, 1, 0, 0, 0);
    showDate("dt2", dt2);

    DateTime dt3 (2009, 1, 2, 0, 0, 0);
    showDate("dt3", dt3);

    DateTime dt4 (2009, 1, 27, 0, 0, 0);
    showDate("dt4", dt4);

    DateTime dt5 (2009, 2, 27, 0, 0, 0);
    showDate("dt5", dt5);

    DateTime dt6 (2009, 12, 27, 0, 0, 0);
    showDate("dt6", dt6);

    DateTime dt7 (dt6.unixtime() + 3600); // One hour later.
    showDate("dt7", dt7);

    DateTime dt75 = dt6 + TimeSpan(0, 1, 0, 0); // One hour later with TimeSpan addition.
    showDate("dt7.5", dt75);

    DateTime dt8 (dt6.unixtime() + 86400L); // One day later.
    showDate("dt8", dt8);

    DateTime dt85 = dt6 + TimeSpan(1, 0, 0, 0); // One day later with TimeSpan addition.
    showDate("dt8.5", dt85);

    DateTime dt9 (dt6.unixtime() + 7 * 86400L); // One week later.
    showDate("dt9", dt9);

    DateTime dt95 = dt6 + TimeSpan(7, 0, 0, 0); // One week later with TimeSpan addition.
    showDate("dt9.5", dt95);

    DateTime dt10 = dt6 + TimeSpan(0, 0, 42, 42); // Fourty two minutes and fourty two seconds later.
    showDate("dt10", dt10);

    DateTime dt11 = dt6 - TimeSpan(7, 0, 0, 0);  // One week ago.
    showDate("dt11", dt11);

    TimeSpan ts1 = dt6 - dt5;
    showTimeSpan("dt6-dt5", ts1);

    TimeSpan ts2 = dt10 - dt6;
    showTimeSpan("dt10-dt6", ts2);
}

void loop () {
}

It means that dt is passed as a reference:

https://www.ibm.com/docs/en/zos/2.4.0?topic=calls-pass-by-reference-c-only

Addendum

https://isocpp.org/wiki/faq/references

https://cplusplus.com/articles/1075fSEw/

There is no separate 'Arduino programmer' terms. Arduino environment uses C/C++ as a language for program.

2 Likes

Why would a const value be passed as a reference ?

1 Like

It's more efficient then passing a large data structure (and/or one that is expensive to copy) by value on the stack.

Same idea as:

void function(const *VeryBigType data)  {
}
1 Like

Thanks. I read all three but the first of your two addenda came closest to my comprehension level as an ‘Arduino programmer’. Enough of a grasp of references for me to proceed anyway!

1 Like

Well, there is for me!

And I had understood that Arduino does not use the entire C++ language in its libraries and syntax? I believe it also uses some C?

C++ like Unix is written in C.

1 Like

C and C++ share a very large common subset.

Most C programs can be compiled as C++ and the behaviour would be the same.

1 Like

Yes, I see that you are holding on to being an Arduino programmer and not learning anything extra about C++ :slight_smile:

A tad patronising, I think. I'm steadily learning a little more - but that doesn't make me anything approaching a serious C++ programmer. And why do you presume I want to invest the time to become one?

Check this out:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-inout

I also found this discussion interesting: https://cplusplus.com/forum/beginner/275072/

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f16-for-in-parameters-pass-cheaply-copied-types-by-value-and-others-by-reference-to-const

Which is what @gfvalvo said

I read that & as "address of" - the sort of thing you would set a pointer * to.

That's what the '&' means when you're calling a function:

  functionThatTakesAnAddress(&varaible);

But it means a C++ Reference when you're talking about the parameters of a function:

void functionThatTakesAReference(int &var) {
  
}

Pointers and References are related, but they are definitely not the same thing.

1 Like

Refresh me here about pointers and references in C and how a C++ Reference differs?

Every now and then you should take a look at a textbook:

https://www.learncpp.com/cpp-tutorial/introduction-to-pointers/

2 Likes

They're THE SAME then.

SOMETIMES they can provide EQUIVALENT functionality. In OP's example, the library's author could have chosen to have the function in question accept a 'const *':

void showDate(const char* txt, const DateTime *dt)

But generally speaking, no. There are applications in more advanced C++ coding where only a Reference will work.

2 Likes

Nice to know since I cut my C teeth using * and & in the 80's while for money, people I got work from Had To Have BASIC and I had bills that Had To Be paid.