473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about C Functions

If I have the function:

int f(int (*h)(int)) {
return (*h)(13);
}

What exactly does (int (*h)(int)) do? So it's taking a pointer but
what's with the two ints? Thanks.

Nov 1 '06 #1
33 1532
jobo wrote:
If I have the function:

int f(int (*h)(int)) {
return (*h)(13);
}

What exactly does (int (*h)(int)) do? So it's taking a pointer but
what's with the two ints? Thanks.
f is a function returning int. It takes as an argument a pointer
to a function returning int, which function takes an int as an
argument. f's argument is called h. f calls the function
pointed to by h with an argument of 13.

Look for the program called cdecl, which helps explain C
declarations.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 1 '06 #2
jobo wrote:
If I have the function:

int f(int (*h)(int)) {
return (*h)(13);
}

What exactly does (int (*h)(int)) do? So it's taking a pointer but
what's with the two ints? Thanks.


Incrementally read as:

h -- a pointer
(*h) -- whose dereferenced value
(*h)(int) -- is a function that accepts a single int as a
parameter
int (*h)(int) -- and returns an int
Thus h is a pointer to a function with the aforementioned parameter
and return types. For example, h can be used to point to the 'toupper'
function defined in the C standard:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main (void)
{
int (*h)(int);
h = toupper;
printf ("%c programming is hard!\n", (*h)('c'));
return 0;
}

--
Hope this helps,
Steven

Nov 1 '06 #3
"jobo" <jo*****@gmail. comwrites:
If I have the function:

int f(int (*h)(int)) {
return (*h)(13);
}

What exactly does (int (*h)(int)) do? So it's taking a pointer but
what's with the two ints? Thanks.
f is a function that returns a result of type int. It has a single
parameter, "h", of type int (*)(int). In other words, h is a pointer
to a function; that function has a single parameter of type int, and
returns a result of type int.

There's a program called "cdecl" that's often helpful for this kind of
thing (though it doesn't like the parameter name):

% cdecl
Type `help' or `?' for help
cdeclexplain int f(int (*h)(int))
syntax error
cdeclexplain int f(int (*)(int))
declare f as function (pointer to function (int) returning int) returning int
cdecl>

I got it from
<http://www.ibiblio.org/pub/Linux/devel/lang/c/cdecl-2.5.tar.gz>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 1 '06 #4
at************* @gmail.com wrote:
jobo wrote:
>What exactly does (int (*h)(int)) do? So it's taking a pointer but
what's with the two ints? Thanks.

Incrementally read as:

h -- a pointer
From 'h' all we can tell is that 'h' is an identifier.
(*h) -- whose dereferenced value
Only now can we see that 'h' is a pointer whose dereferenced value ...
(*h)(int) -- is a function that accepts a single int as a parameter
int (*h)(int) -- and returns an int
I would read it like this:

h -- object
*h -- pointer
(*h) -- pointer
int (*h)(int) -- pointer to function (int) returning int

--
Simon.
Nov 1 '06 #5
jobo:
int f(int (*h)(int)) {
return (*h)(13);
}

The parentheses and dereference are redundant.

int f(int (*const Func)(int))
{
return Func(13);
}

--

Frederick Gotham
Nov 1 '06 #6
Frederick Gotham wrote:
jobo:
>int f(int (*h)(int)) {
return (*h)(13);
}


The parentheses and dereference are redundant.

int f(int (*const Func)(int))
{
return Func(13);
}
For some reason cdecl chokes on your definition above. When I give it
just the parameter, it's fine:

cdeclexplain int (* const Func)(int)
declare Func as const pointer to function (int) returning int

But when I give it the whole thing (minus the name of the parameter), it
chokes:

cdeclexplain int f(int (*const)(int))
syntax error

On further exploration it seems to choke on all const pointers as
parameters:

cdeclexplain int f(int *const)
syntax error

However it works properly in the other direction:

cdecldeclare f as function (const pointer to function (int) returning
int) returning int
int f(int (* const )(int ))

--
Simon.
Nov 1 '06 #7
Simon Biber:
For some reason cdecl chokes on your definition above.

Sorry, I don't know what cdecl is.

<snip cdecl stuff>

At first glance, all the ones that give syntax errors are the ones which
cannot serve as standalone declarations, e.g.:

int main(void)
{
int (*const)(int);
}

--

Frederick Gotham
Nov 1 '06 #8
Frederick Gotham said:
Simon Biber:
>For some reason cdecl chokes on your definition above.


Sorry, I don't know what cdecl is.
Read K&R2 more closely.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 1 '06 #9
On Wed, 01 Nov 2006 15:43:46 GMT, in comp.lang.c , Frederick Gotham
<fg*******@SPAM .comwrote:
>Simon Biber:
>For some reason cdecl chokes on your definition above.


Sorry, I don't know what cdecl is.
This is obvious from one of your comments in a different thread.
cdecl is a programme that describes a C construct in human.

# cdecl explain int arr[2][2]
declare arr as array 2 of array 2 of int

I strongly recommend you obtain and play with it, its jolly useful not
to mention helping clarify some other questions.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Nov 1 '06 #10

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

Similar topics

51
4294
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class, a method (event) needs to be able to invoke methods of the second class. With static classes its possible but this is not desirable. There's obviouly some visibility problem I am not familiar with. It is not a parent-child relationship since...
3
1670
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can you please verify for me? Usually, I write function that is about 100 lines. I would need 20 parameters which they are all reference to global variable inside struct. For best optimization, it does not require to create stack frame because...
55
2784
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
11
4266
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
3
1206
by: Roman Mashak | last post by:
Hello, All! As far as I understand, function declaration with 'static' keyword limits the access to these functions within the file where it declared. So, my question is: where can it be used in real applications? Thanks in advance. With best regards, Roman Mashak. E-mail: mrv@tusur.ru
3
2020
by: Mark Fox | last post by:
Hello, I am porting some old ASP 3.0 VBScript code to C#.NET and am not sure what the equivalent C# functions are for a couple VBScript functions. So my first question is: 1) Is there somewhere that lists old VBScript functions and their equivalent C# functions?
6
2115
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
5
2364
by: Urs Beeli | last post by:
I have a question regarding errno. If I understand it correctly, including <errno.h> allows me to check "errno" for error values that some standard library functions may set. This brings up some questions: - As I understand it, most functions only set errno in an error case. I take this to mean, that on success, errno is not necessarily set to EOK and I would either need to set errno to EOK before calling the function in question to...
7
1875
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that shows the alert. What is the essential reason? function () { alert('hi mom'); }(); function () { alert('hi dad'); }(8); var x=function () { alert('hi bro'); }();
12
3015
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
0
8921
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9202
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.