Can you call sizeof() on an array that is passed to a function as an argument?

Hi,

In C, can you pass an array to a function and then called sizeof() inside that function on the array?

For example:

void myFunction(int *param) {
   
int size = sizeof(param);

}

thanks

Wouldn't it have been faster to try it than to create the post?

In C, can you pass an array to a function and then called sizeof() inside that function on the array?

You can. But, you get the size of the pointer, not the size of the memory that the variable points to. And, perhaps now it is obvious why. A pointer points to a memory location. That the memory location may correspond to the first element of an array is irrelevant.

C library functions that take an array have an additional parameter for the size of the array, or the array has a specific value in an element of the array (e.g. 0 in C strings) to mark the end of the array. There must be a reason for this.

ultrasonicbananna:
In C, can you pass an array to a function and then called sizeof() inside that function on the array?

Yes, but you will only get the right answer if you already know the right answer.

ultrasonicbananna:
For example:

void myFunction(int *param) {

int size = sizeof(param);

}

You are not passing an array. You are passing a pointer to an integer. The size will be 2. To pass an array, you use an array:

void myFunction(int param[30]) {
   
int size = sizeof(param);

}

In this case you have to tell it the size of the array if you want the size to be right. In this case it will be 60 (bytes).

No. When you pass an array to a function, the array is evaluated - it turns into a pointer to it's zeroth element.

JaBa:
Wouldn't it have been faster to try it than to create the post?

Agreed.

ultrasonicbananna:
In C, can you pass an array to a function and then called sizeof() inside that function on the array?

Yes you can do that. What result were you expecting?

To expand on rely #3 by @vaj4088

void someFunc (int *ptr, byte numElements)
{
  ...
  ...
}


void loop()
{
  int positions[32];

  somefunc(positions, sizeof(positions) / sizeof(positions[0]));

}