c - Why does this execute a jump instruction? -
i have seen interesting c code in boot loader of small embedded product.
the code consists of 2 parts: boot loader , main code. start of main code address stored in function pointer. below code i'm talking about
typedef struct { uint32_t myotherfield; void (*maincodestartaddress)(void); } mystruct; mystruct mystruct = {0x89abcdef, 0x12345678}; void main(void) { // code ... mystruct.maincodestartaddress(); // jump start of application }
i can't figure out why works? why machine go application code? appreciated.
the construct void (*maincodestartaddress)(void)
declares function pointer named maincodestartaddress
. point function taking no arguments , doesn't return anything.
you can assign function of same type function pointer, example:
void some_function(void) { // useful } mystruct.maincodestartaddress = some_function;
then can call this:
mystruct.maincodestartaddress(); // same calling some_function();
in example fixed address assigned function pointer, when call function points jumps address , executes it.