Passing a time_t array as a function parameter

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);
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 ?

• you must pass the size as an additional parameter. the size of the array entering the function is unknown at runtime
– Juraj
Sep 19 '20 at 11:04
• @Juraj - Meaning that it is ref OK but its size can't be calculated ?
– guyd
Sep 19 '20 at 11:06
• pointer or reference is the beginning address only
– Juraj
Sep 19 '20 at 11:07
• 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 '20 at 11:10
• 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 '20 at 11:15

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))

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);
• 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. Sep 19 '20 at 13:29
• @DuncanC In this instance that is true, but it is seldom the case.
– Majenko
Sep 19 '20 at 14:08
• 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. Sep 19 '20 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 '20 at 15:19
• I like your edit, proposing the best option. Why are people downvoting this answer? It is the correct answer. Sep 19 '20 at 17:43
{"ft_lang_label":"__label__en","ft_lang_prob":0.8251968,"math_prob":0.9133292,"size":745,"snap":"2022-05-2022-21","text_gpt3_token_len":205,"char_repetition_ratio":0.109311745,"word_repetition_ratio":0.0,"special_character_ratio":0.2872483,"punctuation_ratio":0.16778524,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95992744,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-24T00:28:54Z\",\"WARC-Record-ID\":\"<urn:uuid:0cada424-7822-4ae8-9c48-137391d93e2e>\",\"Content-Length\":\"148887\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bc0880ef-1b99-46de-9060-e0ecfd1ba322>\",\"WARC-Concurrent-To\":\"<urn:uuid:3181ae2e-c5f8-43e8-83df-b5c50ff3d613>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://arduino.stackexchange.com/questions/78096/passing-a-time-t-array-as-a-function-parameter\",\"WARC-Payload-Digest\":\"sha1:YMRIIXJPSH72MB23OWMO7U647JP7VFUN\",\"WARC-Block-Digest\":\"sha1:OIBKDB6YHWL7WPAISF4LZCL6UXFAY2NC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320304345.92_warc_CC-MAIN-20220123232910-20220124022910-00578.warc.gz\"}"}