Purdum's Date library example

Hi to everyone,
first I would like to excuse me for asking newbie questions.

I'm studying the excellent "Beginning C for Ardunino" (Auth: Jack Purdum), and I found something I can't understand in the examples of the chapter 12, where he shows an example-library for calculating the Easter dates.

In particular...

Header file "Dates.h"

class Dates
{
public:
#define ASCIIZERO 48 // character for 0 in ASCII
struct easter {
int month;
int day;
int year;
int leap;
char easterStr[11];
};
struct easter myEaster;
// Function prototypes:
int IsLeapYear(int year);
void GetEaster(Dates *myEaster);
};

Then, he says: "The GetEaster() function is passed a pointer to an easter structure."

But, in the "Dates.cpp" file, the code for the function is:

void Dates::GetEaster(Dates *myData){ // This is line 44
int offset;
int leap;
int day;
int temp1, temp2, total;
myData->myEaster.easterStr[0] = '0'; // Always a '0'
myData->myEaster.easterStr[2] = '/'; // Always a '/'
myData->myEaster.easterStr[3] = '0'; // Assume day is less than 10
myData->myEaster.easterStr[10] = '\0'; // null char for End of string
offset = myData->myEaster.year % 19;
leap = myData->myEaster.year % 4;

...and so on

So, my questions are:
1- why in the header file, the function prototype has "Dates *myEaster" as argument, but then the book says that I have to pass a pointer to an easter structure?

2- the name in the argument of the function prototype (*myEaster) is the same of the variable (struct easter type) defined few lines before: is this mandatory or I could write the function prototype like this (for example)?

void GetEaster(Dates *myPtr);

3- this argument, points to what? To a Dates class or to the struct easter variable?

4- in the function code, the line

myData->myEaster.easterStr[0] = '0';

works only if *myData is a pointer to the Dates Class or to the struct easter variable?

5- in the ".ino" code, the function GetEaster() is called as:

myDates.GetEaster(&myDates);

And I'm passing the address of the Dates class, not the myEaster object. So, i'm looping back to the first question...

Sorry if I wrote unclear or stupid questions. But I hope you can help me to understand better.

1- why in the header file, the function prototype has "Dates *myEaster" as argument, but then the book says that I have to pass a pointer to an easter structure?

The function takes a pointer to an instance of a class that contains a myEaster struct member. The name of the instance in the argument list in the header file is irrelevant. The name chosen is usually one that makes the meaning of the argument clear to the caller, not to the code.

Then, he says: "The GetEaster() function is passed a pointer to an easter structure."

That is wrong. It takes a pointer to an nstance of a class that contains a myEaster struct member.

2- the name in the argument of the function prototype (*myEaster) is the same of the variable (struct easter type) defined few lines before: is this mandatory

No. See my first comment.

or I could write the function prototype like this (for example)?

Yes, but. The myEaster name sort of makes sense. The myPtr name makes no sense. The * says that the variable is a pointer. The variable is obviously yours. The Ptr in the name does not tell anyone what it is a pointer to.

3- this argument, points to what? To a Dates class or to the struct easter variable?

See my first comment.

4- in the function code, the line ... works only if *myData is a pointer to the Dates Class or to the struct easter variable?

It works only if the variable is a pointer to an instance of a class that has a struct easter member.

5- in the ".ino" code, the function GetEaster() is called as ... And I'm passing the address of the Dates class

No, you are not. You are passing the address of an object that is an instance of a class that... Big difference. The & part makes a pointer out of the address of the object, making it an instance.

Dates *myEaster;

is a pointer of type Dates called myEaster

a pointer of type Dates points to a Dates object. In the wild that pointer may only point to a Dates object or nothing (i.e. nullptr).

Dates *myPtr

is a poorly named pointer of type Dates called myPtr

void GetEaster(Dates *myPtr);

myPtr will point to whatever argument gets passed to the function, like for example:

myDates.GetEaster(&myDates);

is a pointer to the myDates object.

the address-of operator is how you turn the object into a pointer to the object, as demonstrated in that code.

I have to say, the variable names (if they are used that way in the book) are rather suckish.

Ok,
thank you for your help. :slight_smile:
I was really confused, due to the same name used for both the variable and the argument.
Now the whole example looks better, for me!

Thank you!