I am an electronics guy so, treat me well, please.
I was reading this article about real time operating systems for 8-bit microcontrollers. I came across this function with a weird looking argument. I could not understand what it is doing. I know that void means “no type”. I am guessing that (*Task) is the casting. I really have no idea what those brackets do after that.
What does this function’s argument include?
Also, I couldn’t understand what does *(int*)((NewTCB->Stack) + (STACK_DEPTH-2)) = (int)Task; do?

void (*Task)()is actually a function pointer. Basically it is saying: “the parameter name isTaskand it is a function which returnsvoidand (since this is c not c++) takes any number of arguments.So you could call it like this:
Of course, it would also be safe to write
void my_task(void) {as well. When coding c, I personally prefer to explicitly say “there are no parameters”Finally,
*(int*)((NewTCB->Stack) + (STACK_DEPTH-2)) = (int)Task;is doing some casting magic.Let’s disect it:
(int)Taskis first convertingTaskto anint(which is questionable, but probably ok for your particular architecture/OS. Personally, I would use alongto be safe).((NewTCB->Stack) + (STACK_DEPTH-2))is just doing some simple arithmetic onNewTCB->Stackto get a pointer to a location in theTCB‘s stack.*(int*)says “convert this to anint *and then deference (read or write) the location it points to.We could write this more simply as follows:
which is probably more clear.
FOLLOW UP: I’d like to point out how I use tend to use function pointers since the syntax can be a bit confusing sometimes. And I find this approach to be very helpful. Basically I like to create a
typedeffor the function pointer and use that instead, I find it a lot easier to get right:For example:
Then later…