passing array to function changes it's size??


I'm having a strange problem with passing a character array to a function and then checking the array's size in the function using the sizeof() utility.

The code is very simple. I declare a char array with some characters in it, check it's size, send it to a function and check it's size again. Yet when I check the passed array size it doesn't match the size before it was sent.

char text_to_send[]  "hello !";

Serial.print("sizeof text_to_send is ");



void my_function(byte type, char text[]) {

  Serial.print("sizeof text is ");


When the above is run I get the following in the console:

sizeof text_to_send is: 8 sizeof text is 4

I understand that there are ways around this but I do not understand why the size would change once passed to the function.

Any ideas?

This is on an arduino due w/ 1.5.6-r2 environment.

Look carefully at what sizeof does. Your array does not change size.

I understand that it will return a count of bytes, so if larger datatypes were used ( int for example ) you'd get a byte size double that of your int array size.

The char used is a byte size, however and sizeof() does return the proper size when called outside the scope of my_fuction().

Why would it change when called inside of my_function()?

sizeof returns the number of bytes making up the data type you give it. How many bytes in a pointer?

What's wrong with strlen?

So they array is treated as a pointer and returns the size of the pointer when called in the function and treated as an array and returns the size of the array when called in the main loop?

Inside the function, you're giving sizeof a pointer, so that's what it returns the size of.


So in main char[] is treated as an array and sizeof will return size of array in bytes and inside the function char[] will be treated as a pointer and return the size of the pointer, not the array. I understand now, just wasn't aware it would treat what looks to be the same datatype differently.

Thanks for the help!