Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can someone explain what is (void (*) (void))((uint32_t)&__STACK_END)?

This is some startup file excerpt with interrupt vectors.

#pragma DATA_SECTION(interruptVectors, ".intvects")
void (* const interruptVectors[])(void) = 
{
 (void (*) (void))((uint32_t)&__STACK_END),
 resetISR,
 nmi_ISR,
 fault_ISR,
 ... /* More interrupt vectors */

void (* const interruptVectors[])(void) – is a array of function pointers that must contain function names, but I can’t understand (void (*) (void))((uint32_t)&__STACK_END) syntax.

(void (*) (void)) looks like a pointer to a function that returns nothing, without arguments and has no name. Is it possible?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(uint32_t)&__STACK_END is a pointer.
And why are function pointer and pointer together?

Thanks in advance!

>Solution :

This looks like interrupt vector table for ARM processor or similar. Interrupt vector table contains addresses of interrupt handlers, so it is essentially an array of function pointers.

First entry of this table is initialization value for stack pointer. It’s obviously not function pointer, but data pointer, so some type conversion is needed. Not because processor cares about types, but because C does.

So &__STACK_END is presumable some pointer type which points to data address at the end of stack. This is then converted to plain 32-bit number, and finally converted to function pointer.

It might have been possible to skip first cast to uint32_t and cast directly from data pointer to function pointer, but it is perhaps done to make sure that conversion is done correctly on given compiler.

Stricly speaking in C standard this conversion from data pointer to function pointer is not legal, even with integer cast in the middle. However, most embedded compilers can do this, because this is typical thing you need to do.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading