Go Down

Topic: sizeof() problem (Read 858 times) previous topic - next topic

Tim Stamp

Hi folks,

Code: [Select]
int variables[5] = {a,b,c,d,e};
Serial.println( sizeof(variables) );

Why does this block of code return 10?

Thanks in advance for your help.


Because you have an array of 5 ints which are two bytes each.  5*2 = 10.


Tim Stamp

Ahhhh that explains it... so is there a function to return how long an array is?

.. I think I'll start using char[] arrays seeing as the PWM pins only have 256-bit resolution. integers just seem too wasteful now.



I think there was a discussion on that not too long ago, but I think sizeof(a)/sizeof(a[0]) will do it.



Code: [Select]

#define countof(a)   ( (sizeof(a) / sizeof(*(a)) )

I usually define this early on.  Then you can use countof(myarrayname) and get the number of elements in the array.  Note that you cannot tell how big an array is if you use a pointer instead.

Tim Stamp

ok, thanks for the ideas folks

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131