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.
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".
So as I did write in my previous post - consider taking some steps back and spend some time with a good tutorial. You really do need to understand basic C concepts before you can manage any big wonders in the embedded world.
Another thing - it's easier to learn to program in C on a PC. And there are good and free tools to use - complete IDE with editor, compiler and debugger. Debugging a PC program means the debugger have an almost infinite bandwidth to display variable and memory content while debugging. The same isn't true when trying to debug an embedded chip where all dumped data from the chip needs to be sent out over some serial link of some type. The PC can also manage a basically infinite number of breakpoints when debugging - the microcontrollers can only do a few hardware breakpoints and then requires reprogramming of the flash memory to set more breakpoints.
You don't start driving MC on a 1300cc Hyabusa - you start with a light MC or probably before that a pedal bike. Why jump in at the deep end before you can swim?
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