I'm reading a library file timer.h
for STM core library that I just installed in Arduino hardware folder.
Of course, there has to be a lot of questions for me as a beginner about many approaches/stratgegies that programmers rule in writing the code.
One thing is that calling a function with a function pointer that only call one function, but the difference is that the function pointer is declared/defined as an inline function.
Edit: I deleted the code and posted a link for it in github.
OK, the part that I started to think about starts from line 595.
It's functions declarations.
In line 598, it's a declaration for a function that receiver a pointer to a function.
In later lines, starting from line 634, these functions have only one line to execute. So my question is why to develop a function to only execute one line ?
Why they didn't just put that line directly instead of putting it inside function ?
1 Answer 1
bb_perip
is a function that returns a pointer to a bit from a register that is related to a specific timer peripheral (there's more than one). That pointer is then dereferenced with the *
operator to make *bb_perip(...)
.
What bb_perip
returns is dependent on what timer you are using and what value you are interested in.
bb_perip
is not a pointer to a function, it is a normal function that happens to return a pointer.
-
Something new to me is that there is an integer passed to this function ! It's my first time to encounter this feature.
*bb_perip(&(dev->regs).bas->CR1, TIMER_CR1_CEN_BIT) = 0;
there is a 0 passed to this function, how this is done ?R1S8K– R1S8K2020年03月10日 11:36:56 +00:00Commented Mar 10, 2020 at 11:36 -
1No, the integer is not being passed to the function. The integer is being assigned to the dereferenced pointer returned from the function.Majenko– Majenko2020年03月10日 12:08:12 +00:00Commented Mar 10, 2020 at 12:08
-
oooh I'm sorry my mistake I wanted to say "assigned" not "passed" :) totally different approaches. Yeah, I meant it's a new feature to me to assign anything to a function.R1S8K– R1S8K2020年03月10日 12:13:26 +00:00Commented Mar 10, 2020 at 12:13
-
Or because it's a function pointer, where the pointer is a variable basically, so I can assign a value for it. is that correct ?R1S8K– R1S8K2020年03月10日 12:14:16 +00:00Commented Mar 10, 2020 at 12:14
-
1You are not assigning anything to the function. The function returns a pointer to somewhere in memory. You take that pointer, then use it to place a value into memory at that address. Once the function has returned the pointer it has no further role to play. You can just as well use (in pseudocode)
*func() = 4
as you canpointer = func(); *pointer = 4;
- all you're doing is leaving out the middleman.Majenko– Majenko2020年03月10日 12:21:11 +00:00Commented Mar 10, 2020 at 12:21
Explore related questions
See similar questions with these tags.
bb_perip
is not a function pointer, I think, but a function, that returns a pointer to a variable. Though I cannot be sure from this snippet of code alone...\Program_Files\Arduino\hardware\Arduino_STM32-master\STM32F1\system\libmaple\include\libmaple
for example depending on the root file location on my computer.