Data type byte & sizeof

Hello everyone!

I have a question: i want to use sizeof to measure an array of bytes. does the data type byte occupy 1 byte in memmory like a char or more?

Like for example:

byte Array1[] = {1,2,3};
for (int i=0; i<= sizeof (Array1); I++)
{
   Serial.print = (" i = ");
   Serial.println = (i);
}

Will the printing stop at number 3 ?

Thank you !

Try it.

Surprisingly enough a byte occupies exactly one byte

The safest way to determine how many bytes are used by an array of any data type is

sizeof(theArray) / sizeof(theArray[0])

It will. (If your code stood a chance of compiling)
But you want to stop at two.

I know it will not compile like this but you get the point.

Why do i want to stop at 2 ?

I remember that in char arrays i need to subtract 1 due to the null terminating character.

UKHeliBob:
Surprisingly enough a byte occupies exactly one byte

Thats why its called "byte" yes :slight_smile:

HellasT:
Why do i want to stop at 2 ?

Because 2 is the index of the last element of the array.

TheMemberFormerlyKnownAsAWOL:
Because 2 is the index of the last element of the array.

Silly me...

THANKS!

UKHeliBob:
The safest way to determine how many bytes are used by an array of any data type is

sizeof(theArray) / sizeof(theArray[0])

So you divide the number returned by sizeof by the number returned by sizeof from the first element in the array. So if the first element in the array occupies 1 byte then x/1 will always be x and so on.
very smart.

As I said, it works for any data type, and if you assign the result to a const variable with a sensible name you can use it throughout the program

UKHeliBob:
As I said, it works for any data type, and if you assign the result to a const variable with a sensible name you can use it throughout the program

I understand. Also using names that make sense is indeed a must in programming. Even i, the biggest noob in this forum, know that.

UKHeliBob:
Surprisingly enough a byte occupies exactly one byte

The safest way to determine how many bytes are used by an array of any data type is

sizeof(theArray) / sizeof(theArray[0])

That's the number of ELEMENTS in the array, not the number of BYTES in the array.

The number of bytes used by the entire array is always just 'sizeof(theArray)'.

Which one of the following two quotes is correct?

UKHeliBob:
The safest way to determine how many bytes are used by an array of any data type is

sizeof(theArray) / sizeof(theArray[0])

MHotchin:
That's the number of ELEMENTS in the array, not the number of BYTES in the array.

The number of bytes used by the entire array is always just 'sizeof(theArray)'.

Verification:
1. This is an array:

float x[] = {1.20, 1.57, 6.5};

2. The 'numbers of memory locations occupied by the array' (the size of array) of Step-1 is: (number of elements/members present in the array)(number of memory locations occupied by an element of the array) = 34 = 12 bytes memory locations. So, the 'size of the array x[]' is 12. To note that every float variable occupies 4-byte memory locations.

Serial.print(sizeof(x));    //shows: 12

3. The number of elements/members (the data items) present in the array of Step-1 is 3.

Serial.print(numberOfBytesOccupiedByArray/sizeOfEachElement);   
==> Serial.print(sizeof(x)/sizeof(x[0]);   //shows: 3

Conclusion:
The number of bytes used/occupied by the entire array is always just 'sizeof(theArray)'
and not
'sizeof(theArray) / sizeof(theArray[0])'

That's the number of ELEMENTS in the array, not the number of BYTES in the array.

You are right