Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By viktak
#85841 Hi All,
I have a function like this:

Code: Select allvoid myFunction(uint16_t buf[]) {
uint16_t size = sizeof(buf)

However, the size that gets calculated is wrong, shows 4 instead of the correct value (which is around 100).

When I use the the same line
Code: Select alluint16_t size = sizeof(myArray)
in the application, it works fine. So I guess I don't call the function correctly.

Could someone with more experience in c help me out?

Thanks in advance!

User avatar
By quackmore
#85842 you are doing everything right

the point is that when you pass an array argument to a function
you pass it by reference (the only way)
so "uint16_t buf[]" is not an array but a pointer
hence sizeof give you 4, the size of a pointer

you could change your function prototype as

Code: Select allvoid myFunction(uint16_t buf[], uint16_t size);

and then call it like this

Code: Select allmyFunction(myArray, sizeof(myArray));
User avatar
By quackmore
Code: Select allvoid myFunction(int *buf);
void myFunction(int buf[]);
void myFunction(int buf[10]);

whatever notation you will choose it will be always a call by reference
you can easily double check with sizeof...
and don't forget sizeof will give you the size of the array in bytes, not elements