BACKGROUND:
I'm learning embedded system programming. During the process I have learn that "Pointer" is a most for embedded system. A pointer is a variable declared in C whose value is the address of another variable. And I can manipulate/change the value of this other variable by dereferencing a pointer.
Example:
int *pt; // Integer pointer variable declaration.
float *pf; // Float pointer variable declaration.
int *pt
means that pt
is a pointer variable capable of pointing to variables of type int. On the other hand, the pointer variable fp
can only store the address of a float type variable.
To assign an value (address) to a pointer variable the address operator (&
) must be used.
int var = 20; //Actual variable declaration
int *pt; //Pointer Variable Declaration
pt = &var; //Here with the ampersand (&)operator we denotes an
//address in memory to the pt pointer.
/*Changing the variable value from 20 to 79*/
*pt = 79; //Dereference
printf (“Value of *pt variable: %d\n”, *pt); //Output:79
printf (“Value of var variable: %d\n”, var); //Output:79
These two links were very helpful for me to understand pointers:
Pointer (computer programming)
QUESTION
My question arises when I come across the header file of the Stellaris LM4F120H5QR Microcontroller. This header file define the register locations with memory addresses as follows:
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
When I came across this syntax I was confused if indeed " (volatile unsigned long *)0x400253FC)
" has been define as a pointer and the whole sentence can be interpreted as shows in picture below?
Pointer Dereference Memory Location
If that is not correct, can someone explain the correct way to interpret the register definition for an embedded system header file?
Link for the header file --> Here
this is a correct defiention !! and if you try it it will work just fine .... let me help you in understanding this statment how it work
if you have an a specific address in memory FOR example (this address 0x400253FC) and you like to write value of (50) to the first byte the following code will be wrong
// the following code is wrong
0x400253FC = 50 ;
the above code will give a compile error .. so how to tell compiler to make this (0x400253FC) as a memory address ??, simply will do that with help of casting
(unsinged char *)0x400253FC // cast this number 0x400253FC as char pointer
now you have a pointer so you could dereference it and write the value in memory (where the pointer point to) like this
*((unsinged char *)0x400253FC) = 50; // write the value 50 in address 0x400253FC
some compilers when they make Optimization they will delete this line of code so to prevent the compiler from doing that we add the volatile specifier ... so the expression will be like this
*((volatile unsinged char *)0x400253FC) = 50; // write the value 50 in address 0x400253FC
so this is how to accssess a byte in memory
if you like to access 4 byte in memory so it will be like this
//assuming your compiler consider the long variable as 4 byte
*((volatile unsinged long*)0x400253FC) = 50; // write the value 50 in 4 byte in memory start with address 0x400253FC