Question about C Functions

J

jobo

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.
 
T

T.M. Sommers

jobo said:
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.
 
A

attn.steven.kuo

jobo said:
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;
}
 
K

Keith Thompson

jobo said:
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
cdecl> explain int f(int (*h)(int))
syntax error
cdecl> explain 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>
 
S

Simon Biber

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
 
F

Frederick Gotham

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


The parentheses and dereference are redundant.

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

Simon Biber

Frederick said:
jobo:



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:

cdecl> explain 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:

cdecl> explain int f(int (*const)(int))
syntax error

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

cdecl> explain int f(int *const)
syntax error

However it works properly in the other direction:

cdecl> declare f as function (const pointer to function (int) returning
int) returning int
int f(int (* const )(int ))
 
F

Frederick Gotham

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);
}
 
M

Mark McIntyre

Simon Biber:



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
 
K

Keith Thompson

Mark McIntyre said:
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.

One thing to watch out for is that it allows aliases for certain
things; see the documentation for details. I've run into this myself:

% cdecl
Type `help' or `?' for help
cdecl> declare ptr as pointer to int
syntax error
cdecl> declare p as pointer to int
int *p
 
F

Frederick Gotham

Mark McIntyre:
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.


You're offering armbands to a proficient swimmer.
 
R

Richard Heathfield

Frederick Gotham said:
Mark McIntyre:
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.

You're offering armbands to a proficient swimmer.

No, no, he was offering them to /you/.
 
F

Frederick Gotham

Richard Heathfield:
No, no, he was offering them to /you/.


Is that a challenge? Throw a declaration at me so, one I can't make head nor
tails of.
 
C

Chris Dollin

Frederick said:
Richard Heathfield:


Is that a challenge? Throw a declaration at me so, one I can't make head nor
tails of.

Well, `throw` is of course off-topic here, but:

void yorkshire() { int stomach; }

seems to fit.
 
R

Richard Heathfield

Frederick Gotham said:
Richard Heathfield:



Is that a challenge?

No. It was merely an observation, based on your apparent inability to grasp
the purposes of standardisation and civility.
Throw a declaration at me so, one I can't make head
nor tails of.

I declare that you owe several people in this newsgroup an apology.
 
F

Frederick Gotham

Richard Heathfield:
I declare that you owe several people in this newsgroup an apology.


In the absence of supplying of a declaration which I would not comprehend, I
accept your concession that I have no need for cdecl.
 
C

CBFalconer

Richard said:
Frederick Gotham said:
.... snip ...


I declare that you owe several people in this newsgroup an apology.

That definitely fits, as he has demonstrated his inability to
comprehend that multiple times.
 
R

Richard Heathfield

Frederick Gotham said:
Richard Heathfield:



In the absence of supplying of a declaration which I would not comprehend,
I accept your concession that I have no need for cdecl.

It is clear from your reply that you cannot make head or tail of my
declaration, QED.
 
C

Christopher Benson-Manica

Richard Heathfield said:
Frederick Gotham said:
I declare that you owe several people in this newsgroup an apology.

Again, would it be possible to identify these posts which consist of
nothing more than seemingly hopeless attempts to induce some measure
of civility in Mr. Gotham? I've plonked Mr. Gotham expressly to avoid
as much involvement as possible in these discussions, but short of
either filtering entire threads (not so good) or filtering *your*
posts (very bad), there is little the rest of us can do to steer clear
of these recurring subthreads.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top