Hi there, I am calculating my total bytes for arrays in different data types, that confused me.
For example:
int myint [5]={1,2,3,4};
char buf[25];
Based on what I understand, each element in integer carries 2 bytes, so for the first line, the total bytes will be 4x(2) bytes or 5x(2)bytes? I was confused if the empty elements in array is required to be added.
And for second line, each element in char carries 1 bytes, so the total bytes will be 25bytes or 24 bytes? Even if the numbers of elements I assign later are not reaching 24?
Arrays cannot have “empty” elements, the fifth element of int myint [5]={1,2,3,4}; will just be initialized to zero, but it does exist, so the size will be 10 bytes*.
The size of char buf[25]; will be 25 bytes. If it's a local variable, the 25 elements will be uninitialized. If it's a global, they will be initialized to zero.
(*) on AVR at least, the size of int can be different on different platforms.
The number of elements is what you have in the array definition, so myInt as 5 elements of type int which each take 2 bytes on a uno. So you will have 10 bytes for this array. If you were to skip the 5 and let the compiler decide, it would be 4 int so 8 bytes
Same rule for the char array, you will have 25 bytes
As mentioned the sizeof operator can tell you how many bytes got allocated
Hi~ so if I am putting one element in the array [25] for integer type, how the remaining elements will be then? be initialized zero as well? Then the total bytes will be 50?
Yes, as mentioned whatever you define is the size. And it’s fixed, you can’t change it later on. Indexes run from 0 to that number minus 1 (as we started at 0)
Global and static variables are zero initialized, the local variables are not initialized for you so could be whatever garbage was in memory at the time (on the stack or heap depending how you allocated the memory)
int array[25]; declares an array of 25 integer elements. The first element is array[0] and the 25th (last) element is array[24]. The total number of bytes occupied will be 25 × sizeof(int), or 50 bytes on AVR.
If you want to initialize all elements to zero, use int array[25] {};
The first one will be 5x2 bytes, the second one 25x1 bytes.
Accessing elements however is done by counting from 0, so you access myint with index values 0 .. 4 and buf withe index values 0 .. 24.
As mentioned above, you can use sizeof() to determine the size of any array in bytes. You can use the below macro to determine the number of elements (not bytes) in any type of array.
char buf[5] = "AB";
// is equivalent to
char buf[5] = {'A', 'B', '\0'};
// is equivalent to
char buf[5] = {'A', 'B', '\0', '\0', '\0'}; // 5 elements in total
Arrays of ordinary character types (char,...),... can be initialized from ordinary string literals, optionally enclosed in braces. Successive characters of the string literal (which includes the implicit terminating null character) initialize the elements of the array. If the size of the array is specified and it is larger than the number of characters in the string literal, the remaining characters are zero-initialized.
No, the string literal "AB" has 3 elements: the characters A, B and a terminating null character \0. If you assign a three-character string to an array of 25 elements, the array will have 25 elements, the length of the original string is irrelevant, all “unused” elements of the array are simply set to null (zero).
The comma is inside of the double quotes, it's part of the string literal, it's the character ',' and doesn't have any syntactic meaning.
char array[6] = "AB,AK";
// is equivalent to
char array[6] = {'A', 'B', ',', 'A', 'K', '\0'};