Please explain this code to me

F

fred.zakity

Exec_Mem(unsigned long address)
{
void (* foo) () = void (*) () address; <-----?
foo ();
}

I understand that foo is a function pointer of type void, but what is
the right hand side of the equation - some kind of cast? Can somebody
elaborate for me?

Thanks for your help,
fred.
 
W

Walter Roberson

Exec_Mem(unsigned long address)
{
void (* foo) () = void (*) () address; <-----?
foo ();
}
I understand that foo is a function pointer of type void, but what is
the right hand side of the equation - some kind of cast? Can somebody
elaborate for me?

Yes, it is a cast of the address to become a pointer to a function
returning void.

The casting of a integral value into a pointer to an object type
is implementation defined. The casting of a numeric value
into a pointer to a function type is undefined in the C89 standard,
but it is not a constraint violation so particular implementations
can do whatever they want with it (possibly even something useful.)

The code you have found uses non-standard extensions and is
decidedly not portable.
 
I

Ian Collins

Exec_Mem(unsigned long address)
{
void (* foo) () = void (*) () address; <-----?
foo ();
}

I understand that foo is a function pointer of type void, but what is
the right hand side of the equation - some kind of cast? Can somebody
elaborate for me?
It looks like a syntax error to me. Should it be written as


void
Exec_Mem(unsigned long address)
{
typedef void (*Fn)();

Fn foo = (Fn)address;
foo ();
}

?
 
F

fred.zakity

It looks like a syntax error to me. Should it be written as

void
Exec_Mem(unsigned long address)
{
typedef void (*Fn)();

Fn foo = (Fn)address;
foo ();

}

?

Thanks for your help.
The code is meant to run on an ARM 966, so is probably not meant to be
portable. I'm not sure if there are any syntax errors - I typed it
exactly as I saw it.

fred.
 
I

Ian Collins

*Please* don't quote signatures.
Thanks for your help.
The code is meant to run on an ARM 966, so is probably not meant to be
portable. I'm not sure if there are any syntax errors - I typed it
exactly as I saw it.
The line

void (* foo) () = void (*) () address;

isn't legal C.

The version I posted is legal, but it's result is undefined because it
assumes an unsigned long can be cast to a function pointer.
 
E

Eric Sosman

Exec_Mem(unsigned long address)
{
void (* foo) () = void (*) () address; <-----?
foo ();
}

I understand that foo is a function pointer of type void, but what is
the right hand side of the equation - some kind of cast? Can somebody
elaborate for me?

As it stands, it's a syntax error. With another set
of parentheses

void (* foo)() = (void(*)()) address;

it would be a cast that converts an unsigned long value
into a function pointer. (Specifically, a pointer to a
function that takes a fixed but unspecified number of
arguments of unspecified types and returns nothing.)

Whether the conversion makes sense is beyond the
scope of the C language. On a particular machine it may
do something useful -- then again, it may not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top