0

I'm trying to log reconnections and disconnections of a network. For that I've created 2 time_t arrays to store such occurances:

const int logSize = 10;
time_t conLog[logSize];
time_t disconLog[logSize];

Now, down the road, I have some manipulation and time calculation to do, and for that I wish to pass those log as a variable.

For example - zeroing its values:

void initLog(time_t log[], time_t init_val = 0)
{
        int sizeLog = sizeof(log) / sizeof(log[0]);
        Serial.print("size: ");
        Serial.println(sizeLog);
        for (int i = 0; i < sizeLog; i++)
        {
                log[i] = init_val;
        }
}

in order using it in a simple way : initLog(conLog); But unfortunately this is wrong.

I've tried to replace time_t log[] with time_t &log - but still no luck.

What do I do wrong ?

7
  • you must pass the size as an additional parameter. the size of the array entering the function is unknown at runtime
    – Juraj
    Sep 19, 2020 at 11:04
  • @Juraj - Meaning that it is ref OK but its size can't be calculated ?
    – guyd
    Sep 19, 2020 at 11:06
  • pointer or reference is the beginning address only
    – Juraj
    Sep 19, 2020 at 11:07
  • 1
    so how would the size be calculated? there is no information about the end address or length. the way you calculate the size works for a specific array when the compiler knows the definition of the array. but array of any length can be passed to this function
    – Juraj
    Sep 19, 2020 at 11:10
  • 2
    for char array that is a convention. for array of pointers you could use a null pointer as mark. here you could choose some time_t value as end mark, but generally all have a meaning.
    – Juraj
    Sep 19, 2020 at 11:15

1 Answer 1

2

When you pass an array to a function it "collapses" the array into just a pointer. All the receiving function knows is the type of the contents of the array (time_t) and where it starts in memory - it knows nothing about the size of the array.

The sizeof() call just gives you the size of that pointer, not the size of the memory it points to.

sizeof() is not actually a function, it's a compile-time construct. It can't know anything about runtime values, only what is there at compile time.

Instead you must pass the length of the array with the array as a separate parameter, which can only be calculated in the same scope as where the array is defined, and then only by using the original name of the array. It is typical to create a macro to do this for you at the time you make the array:

time_t conLog[logSize];
#define CONLOG_SIZE (sizeof(conLog) / sizeof(conLog[0]))

Then pass that to your function:

initLog(conLog, CONLOG_SIZE);

Alternatively, since you already have the size in a constant in this case, you could just use that:

initLog(conLog, logSize);
8
  • 1
    Good answer, except... If you have a constant logSize that you use to create conLog, why write code to calculate it? Just use logSize directly.
    – Duncan C
    Sep 19, 2020 at 13:29
  • @DuncanC In this instance that is true, but it is seldom the case.
    – Majenko
    Sep 19, 2020 at 14:08
  • 1
    Why? For arrays who's size is defined at compile time, using a compile-time constant to express that size is a good, clean way to do it.
    – Duncan C
    Sep 19, 2020 at 14:39
  • @DuncanC Yes. You know that. I know that. Very few Arduino users know that. You have to remember: these are Arduino users, not programmers.
    – Majenko
    Sep 19, 2020 at 15:19
  • 1
    I like your edit, proposing the best option. Why are people downvoting this answer? It is the correct answer.
    – Duncan C
    Sep 19, 2020 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.