Hi all (and Merry Christmas)
How can I use the sizeof function with a pointer?
If I call a function passing a pointer to an array, is there a way to know hot long is that array from inside the function?
void loop()
{
long int anArray[10];
A_Function(anArray);
}
void A_Function(long int* originArray)
{
int x;
x = sizeof(otiginArray); //How can I do this??
}
You can't. You can only get the size of the pointer (2 on an AVR.) You'll have to pass the length separately from "up someplace" where the length is known.
If I call a function passing a pointer to an array, is there a way to know hot long is that array from inside the function?
I don't think so. When you declare an array iits name is a pointer to the first element and its data type tells the compiler how many bytes each element occupies so you can calculate its length.
However, when you pass the pointer to an array the data type is not passed with it so within the function you cannot calculate how many bytes, and hence how many elements there are in the array. The normal solution is to pass the number of elements to the function as part of the function call.