Pointers
Pointers are one of the essential programming features which are available in C.
Pointers are heavily used in embedded C programming to:
- Configure the peripheral register addresses.
- Read/Write into peripheral data registers.
- Read/Write into SRAM/FLASH locations and for many other things.
A pointer points to a memory address where a variable is stored
How to store a pointer inside a program?
<pointer data type> <variable name>
Pointer data types
Data type | Data type |
---|---|
char* |
unsigned char* |
int* |
unsigned int* |
long long int* |
unsigned long long int*` |
float* |
unisgned float* |
double* |
unsigned double* |
Creating a pointer in a script
If we want to assign a specific address to a pointer, we might be tempted to do something like this:
char* address1 = 0x00007FFF8E3C3824
But this won't work because for the compiler, this is a simple number and NOT an address.
To correctly assign an address to a pointer, we shall type cast the address like this:
char* address1 = (char*) 0x00007FFF8E3C3824
where:
- char* is the pointer data type and determines the behavior of the operations carried out on the pointer variable (read, write, increment, decrement).
- address1 is the name of the pointer (in this case this is a pointer variable of type
char*
).
Note that the compiler will always reserve 8 bytes for the pointer variable irrespective of their data type. In other words, the pointer data type doesn't control the memory size of the pointer variable.
So, all of these pointers will be stored in 8 bytes of data:
char* address1; // 8 bytes stored for the address
int* address2; // 8 bytes stored for the address
long long int* address3; // 8 bytes stored for the address
double* address1; // 8 bytes stored for the address