hi Everybody, i copy and paste a line of code bellow as an example:
"#define USB (( USB_TypeDef *) USB_BASE)"
( suppose #define USB_BASE 0x0C000 )
i don't understand the "(( USB_TypeDef *) USB_BASE)"? why the *, the pointer operator, is placed with type inside parenthesis?
thanks, Ras
(( USB_TypeDef *) USB_BASE)
It just states that the value USB_BASE is a pointer of type USB_TypeDef. If you didn't have the *, then it would be saying that the value USB_BASE is of the type USB_TypeDef.
the pointer syntax: type *name for example: int *x1
is the bellowing code true? USB_TypeDef *USB_BASE
Only if you're trying to declare a variable. In which case the variable will be a pointer to a type USB_TypeDef.
The original example was taking a value USB_BASE and telling the compiler to treat it as a pointer to a type USB_TypeDef. By using a typecast.
so, what is the difference between
and
USB_TypeDef *USB_BASE
?
Addresses are numeric (integer) values to specific memory locations.
But the compiler does not like to use integers for performing memory accesses.
So you need to perform a type cast, where you explicitly informs the compiler "the very specific memory area that starts at this very specific address happens to store data of the following type".
That is the purpose of the type cast ((USB_TypeDef *) USB_BASE)
So the compiler then knows that you have a pointer (storing the integer USB_BASE) that will point to a block of memory of the USB_TypeDef type. And this means that if USB_TypeDef is a struct, then the source code can access the individual fields of this struct and the compiler will produce the required code to access the very specific address of the underlying registers stored within this address range.
This is all very very basic C. So if you aren't comfortable with the use of pointers, then I suggest you spend some time with some good programming tutorials. The C language is very heavily focused around pointers so it isn't something you can avoid learning.
Per Westermark!
thank you very much.
" The first is a type cast - telling the compiler to treat an integer as a pointer to a sepcific data type.
The second is a variable declaration. Telling the compiler to allocate the memory for a single variable USB_BASE that is of type "Pointer to USB_TypeDef"."
these solved my problem; type casting.
( i am learning c programming language within my embedded projects. )
with the best regards,
Rasool
View all questions in Keil forum