By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Please explain this code to me

P: n/a
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.

Apr 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>,
<fr*********@gmail.comwrote:
>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 was very young in those days, but I was also rather dim.
-- Christopher Priest
Apr 5 '07 #2

P: n/a
fr*********@gmail.com wrote:
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 ();
}

?
--
Ian Collins.
Apr 5 '07 #3

P: n/a
On Apr 5, 2:23 pm, Ian Collins <ian-n...@hotmail.comwrote:
fred.zak...@gmail.com wrote:
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 ();

}

?
--
Ian Collins.
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.

Apr 5 '07 #4

P: n/a
fr*********@gmail.com wrote:
On Apr 5, 2:23 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>fred.zak...@gmail.com wrote:
>>>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 ();

}

?
--
*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.

--
Ian Collins.
Apr 5 '07 #5

P: n/a
fr*********@gmail.com wrote On 04/05/07 16:10,:
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.

--
Er*********@sun.com
Apr 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.