Hello,
I want to write function which one of input will be an array. How should I declare it to check inside the function what is the size of this array?
I have something like that:
void sendFrame(unsigned int interval, byte* frame)
{
for (int i = 0; i < sizeof(frame); i++)
{
Serial1.write(frame[i]);
delayMicroseconds(interval);
}
Serial1.flush();
delayMicroseconds(interval * 2);
and all the time I have sizeof(frame)=2. I change in declaring from byte* frame to byte frame[], but there the same effect.
There is not another way. Somehow the called function needs to know the size of the array. Given ONLY its starting address, that is not, AT RUN TIME, possible for it to determine. You MUST pass the length or define a sentinel, similar to the NULL in a char array that defines the length of a string, that tells the function when to stop.
Defining a sentinel is not always possible. Passing the length is.
PaulS:
There is not another way. Somehow the called function needs to know the size of the array. Given ONLY its starting address, that is not, AT RUN TIME, possible for it to determine. You MUST pass the length or define a sentinel, similar to the NULL in a char array that defines the length of a string, that tells the function when to stop.
Defining a sentinel is not always possible. Passing the length is.
If you have an actual array, not just a block of memory, you'll have the size at compile time. The article I linked shows how to use either a reference or pointer to an array (not just to the first element) which preserves the size information.
pYro_65:
I have sent you a PM, however the site can generate dodgy pdf files, so you can read this for now.
Thanks a lot. It works now properly. I downloaded library ATL.h and use Template like on this pdf, which you've sent to me. Now everything working as I wanted. Big beer for you!