Read-Write operation on an array
Read and write to an array
Write a program that initializes an array of 10 items to 0xff and then prints the values of each element.
In this article, we are learning array initialization and array read and write.
Create an array of 10 data items of type uint8_t.
uint8_t someData[10];
Let’s initialize all the data items present in this array. There are several ways to do this. You can initialize this array during the variable definition itself.
So, you have to give the assignment operator(=) and you have to use curly braces, and here you can initialize the individual data items.
uint8_t someData[10] = {0xff, 0xff, 0xff};
Here I just initialized the first 3 data items of the array to 0xff. This is perfectly fine.
But, what happens to the remaining data items?
The remaining data items will be automatically initialized to value zeros. That is a very important point.
uint8_t someData[10] = {0xff,0xff,0xff,0,0,0,0,0,0,0}; // This is equivalent to above code.
So, if you just partially initialize the array, then all remaining data items will be automatically initialized to zeros.
#include<stdio.h> #include<stdint.h> int main(void) { uint8_t someData[10] = {0xff,0xff,0xff}; return 0; }
Array declaration
What happens if I remove the number of data item’s information?
uint8_t someData[] = {0xff,0xff,0xff};
Do you think this is a legal ‘C’ statement?
Of course, it is. By using the initializer or initialization information, you are giving enough information for the compiler to calculate the size of the array.
Here during the compilation, the compiler will automatically calculate the size of the array by looking at the information of the initialization.
Here you are looking to initialize 3 data items. The compiler will consider that this array is of three data items. That’s perfectly fair.
That’s why, when you do array definition and initialization in one go, then you can drop a number of data items information, that will be automatically tuned.
uint8_t someData[ ]; // But this is wrong.
If I leave without initializing the array, then this would be an error. Because the compiler couldn’t able to calculate the array size during compile time.
Let’s understand one more type of array declaration or array definition.
Look at the below code snippet, you have the array definition.
#include<stdio.h> #include<stdint.h> int main(void) { int len = 10; uint8_t someData[len]; return 0; }
Array declaration
Here we created a variable int len = 10;
This information need not be a constant, it can be a variable. That’s why, I removed the 10 and in the place of 10 I mentioned the variable name âlenâ, so this type of array is called a variable length array(VLA).
This syntax is introduced as a C99 standard onwards. So, this wasn’t there in a C90 or NCC standard.
We learned how to initialize an array.
Let’s look at how to read and write to individual array elements.
Here, 0XFF are the array elements. 0xE005, 0XE006,..0XE00E are the contiguous memory location addresses, and someData is the base pointer or a reference.
Write a new value to the array element 2.
How do you identify the array element 2 in this case?
0xE006 is the array element 2.
How do you write a new value to the array element 2?
You just increment the pointer. Let’s say someData+1.
So, if you do that it will start pointing 0xE006, and then dereference this whole value, and then write a new value, let’s say 0x9.
*(someData + 1) = 0x9;
If we do this much, then a new value will be stored in the second element. That’s a procedure.
So, by holding the base pointer so you can access any location of the array.
#include<stdio.h> #include<stdint.h> int main(void) { uint8_t someData[10] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; printf("Before: 2nd data item = %x\n", *(someData+1)); *(someData+1) = 0x9; printf("After: 2nd data item = %x\n", *(someData+1)); return 0; }
Here, I initialized the array to 0xff.
And after that base pointer+1 (someData+1), and reference that(*), I wrote the new value 0x9.
Use printf to print the value of the second data item. To access the second element, you can use pointer manipulation. So, *(someData+1) gives us the value of the second data item.
And let’s print once again here, printf(âAfter: 2nd data item = %x\nâ,*(someData+1));
Look at the output here, before the second data item was ff, and after the second data item was 9. Here you modified the second data item using the reference.
So, you just incremented the reference to the next position, and you just dereferenced that and you stored a new value.
But accessing array elements like this would be a tedious process.
There is a shortcut actually. The shorthand method is, just to use the array name someData and after that open the square brackets, and after that mention the offset.
someData[1] = 0x9;
Here 1 is an offset to access the second element. So, we mentioned the offset and the value.
*(someData +1) = 0x9 and someData[1]=0x9, both statements are identical.
This is just for your convenience during programming. So, when you write like someData[1]=0x9 it will be internally converted to *(someData +1) = 0x9 by the compiler.
So, to modify the first data element you should be doing like someData[0]= 0x1. So, that’s the reason why we say array indexing starts from zero. The real reason lies in the pointer manipulation method. The compiler sees this method like *(someData[0])=0x1;
That’s why we always index the array elements from 0.
The first data item we call the 0 elements of the array. The next data item we call the first data element of the array. That’s why we can use this notation someData[ i ], where âiâ ranges from 0 to 9.
So, by doing this we can access different data elements or different data items of the array.
So, now let’s do some experiments.
#include<stdio.h> #include<stdint.h> int main(void) { uint8_t someData[10] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; printf("0th element value = %x\n", someData[0]); printf("0th element value = %x\n", *someData+0)); return 0; }
In printf statements you used both methods. Both those statements are identical.
Now let’s check the output, both are the same. It gives the 0th element value = ff.(Figure 4)
What if we want to print the values of all the elements of this array?
In that case, you can print element by element no problem, but it’s better to use a loop here.
Now, let’s use a while loop, or for loop to print values of all the data elements of this array.
So, for that let me write the printf contents of this array. So, let’s use it for loop.
for(uint32_t i=0; i<10; i++)
Create a variable to index into the array. So, let’s create a variable uint32_t i. Initialize that i=0. For this array index can go from 0 to 9, that’s why, let me use i < 10, and after that i++.
Now here let’s use one more printf, printf(“%x\tâ, someData[i]”); I print all the values in one line, so I would give \t and after that someData[i].
This would print the values of all the data items of the array, where âiâ ranges from 0 to 9.
#include<stdio.h> #include<stdint.h> int main(void) { uint8_t someData[10] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; printf("contents of this array\n"); for(uint32_t i = 0 ; i < 10 ; i++) { printf("%x\t",someData[i]); } return 0; }
The Output is shown in Figure 5.
Let’s modify the second data element.
#include<stdio.h> #include<stdint.h> int main(void) { uint8_t someData[10] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; printf("contents of this array\n"); for(uint32_t i = 0 ; i < 10 ; i++) { printf("%x\t",someData[i]); } someData[2] = 0x33; printf("\n"); for(uint32_t i = 0 ; i < 10 ; i++) { printf("%x\t",someData[i]); } return 0; }
Array indexing starts from the 0th data element. Here I would use someData[2] = 0x33. The modified second data element is 33. The output shown in Figure 6.
So, you can also initialize all the data elements in a loop.
#include<stdio.h> #include<stdint.h> int main(void) { uint8_t someData[10] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; printf("contents of this array\n"); for(uint32_t i = 0 ; i < 10 ; i++) { printf("%x\t",someData[i]); } printf("\n"); for(uint32_t i = 0 ; i < 10 ; i++) { someData[i] = 0x33; printf("%x\t",someData[i]); } return 0; }
I would use someData[i] = 0x33, it prints all the data elements 33.
So, that’s how by using the loop and array indexing, so you can initialize an array to different values.
That’s about array indexing, why array index starts from zero, and accessing different data items of the array using pointer manipulation.
Summary:
Please note that, *(someData) = 10 is identical to someData[0]=10. *(someData+1) = 20 is identical to someData[1]=20, like that.
So, this is array manipulation using pointer access.
That’s about a read and write to an array.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1