473,386 Members | 1,786 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,386 software developers and data experts.

Fuction pointers - Is this legal C?

Can anyone tell me whether the following is legal C?

int foo(int);
int bar(int (*)(int));

int main(void)
{
int x;

x = bar(&foo);

return 0;
}

int bar(int (*foo)(int))
{
/* ... */
}

I had some similar code which compiled fine using 'gcc -Wall', but I'm
concerned about what would happen if I called 'foo' from withing 'bar'.
Would it use the pointer it is passed, or the function declared at the
start, if they were different functions? Or is this undefined
behaviour? What if I used the dereferencing operator when calling foo?

I've changed the name of the pointer 'bar' recieves just to be safe,
but I'm still curious as to what the effects of this sort of code would
be...
Thanks.

--
David Scarlett

dscarlett@_ _ _ _ _ _ _ _
_ _ _ _ _ optusnet.com.au
Nov 14 '05 #1
2 1087

"David Scarlett" <lo**@my.signature> wrote
Can anyone tell me whether the following is legal C?
<snip code>
Would it use the pointer it is passed, or the function declared at the
start, if they were different functions? Or is this undefined
behaviour? What if I used the dereferencing operator when calling foo?


The global name is hidden by the local parameter name. Any references to foo
will act on the parameter, which is standard and correct behavior. It
doesn't matter at all whether you dereference the pointer when calling foo
(except for older compilers that require it), both are allowed and will
still reference the parameter.
Nov 14 '05 #2
On 02 May 2004 09:14:18 GMT, David Scarlett <lo**@my.signature> wrote:
Can anyone tell me whether the following is legal C?

int foo(int);
int bar(int (*)(int));

int main(void)
{
int x;

x = bar(&foo);
The & is unnecessary. A function name without the parenthesized
argument list is the syntactical representation of the address of the
function.

return 0;
}

int bar(int (*foo)(int))
{
/* ... */
}

I had some similar code which compiled fine using 'gcc -Wall', but I'm
concerned about what would happen if I called 'foo' from withing 'bar'.
Would it use the pointer it is passed, or the function declared at the
start, if they were different functions? Or is this undefined
behaviour? What if I used the dereferencing operator when calling foo?

I've changed the name of the pointer 'bar' recieves just to be safe,
but I'm still curious as to what the effects of this sort of code would
be...
Thanks.


<<Remove the del for email>>
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: orion30 | last post by:
I would like to know, if an allocated variable returning by a fuction is unallocated automatically ? If no how to proceed ? Best regards ...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
96
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
6
by: Ben Thomas | last post by:
Hi, Is this code okay and compliant with the standard. As far as I understand it is correct but I just want to make sure. Thank you, Ben. ----
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.