====== ARRAYS ======
==== SYNTAX ====
uint8_t studentsAge[100]; // 100 elements in the array
Things to note:
* //studentAge// is a base pointer to a 100 data items of type uint8_t
* Data type of //studentAge// is **uint8_t***
* Data type of the items stored is **uint8_t**
Since array's name is just a base pointer, you can access N's element by incrementing a pointer and then dereferencing it. Example accessing 1st element (studentsAge[1]):
printf("2nd element is: %u", *(studentAge+1)); // Incrementing a base pointer and dereferencing it
This is the same as:
printf("2nd element is: %u", studentAge[1]); // 1 - is an offset
==== PASSING AN ARRAY TO THE FUNCTION ====
#include
#include
void array_display(uint8_t *pArray, uint32_t nItems);
int main(void)
{
uint8_t data[10] = {0x01, 0x02, 0x03};
uint32_t nItems = sizeof(data) / sizeof(uint8_t);
array_display(data, nItems);
getchar();
getchar();
return 0;
}
void array_display(uint8_t *pArray, uint32_t nItems)
{
for(uint32_t i = 0; i < nItems; i++)
{
printf("Element: %u,\tvalue:%#x\n", i, pArray[i]);
}
}
If we want to print an array with an offset (e.g. starting from 2nd element) - just send the address of the second element to the function:
array_display((data+2), (nItems-2));
Or like this (sending address of the second element):
array_display(&data[2]), (nItems-2));