Array of pointers in c
Like an array of variables, we can also use array of pointers in c.
In this tutorial explains the array of pointers and its application.
Array of pointer
Let's create an array of 5 pointers.
Where arr will hold one integer variable address, arr will hold another integer variable address and so on.
Dereferencing array of pointer
Dereference - Accessing the value stored at the pointer.
Since each array index pointing to a variable's address, we need to use *arr[index] to access the value stored at the particular index's address.
arr[index] will have address
*arr[index] will print the value.
Where arr holding the address of the variable a. i.e. arr = 1024
*arr -> *1024 -> value stored at the memory address 1024 is 10. So, *arr = 10.
Similarly, *arr = 20, *arr = 30 and so on.
Application of array of pointers
Assume that we are going to build an embedded system which uses a different kind of sensors to measure the temperature.
In this case, we can use an array of pointers to hold the memory address of each sensor so that it will be very easy to manipulate the sensor status.
sensor will hold the address of the 1st sensor.
sensor will hold the address of the second sensor and so on.
Since it is an array, we can directly interact with the particular sensor using array index.
we can get the temperature status of 1st sensor using sensor, 2nd sensor status using sensor and so on.