473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Please explain this code to me

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
5 1230
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Randy | last post by:
I have asked a number of questions as a newbie trying to learn Access. Some folks have been very helpful with very direct and simple answers that help alot. Others give me answers with extensive...
12
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
9
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
0
by: Kermitus | last post by:
Hey I was hoping for a little help. I am a Domain reseller for Aplus.net. The problem is they really don't provide an API to automate Domain searches, registering etc... I see much potential in...
5
by: tony | last post by:
I'm using PHP 5 on Win-98 command line (ie no web server involved) I'm processing a large csv file and when I loop through it I can process around 275 records per second. However at around...
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
1
by: kannan1986 | last post by:
now i am doing video stegnography in asp.net with c#. please give a source code or explain how do to that project. or Is there any source code available in websites. please send the source code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.