I don't understand typedef example

M

Michael Melanson

pete said:
The type of a function has nothing
to do with the guts of the function definition.

Two guys named Curry and Howard might have a few things to say about
that. :)

Specifically, Curry-Howard correspondence says that, for a
sufficiently detailed type system, there's a one-to-one matching between
formulae (i.e, functions) and types.

In C this definitely isn't the case, but I thought I'd be pedantic. :)

Cheers!

Michael Melanson
 
J

JosephKK

Keith Thompson said:
Finally, you can declare a typedef either for the function type itself
or for a pointer to the function type:

typedef int func(int, int);

typedef int *func_ptr(int, int);
[...]

Correction: the second should be

typedef int (*func_ptr)(int, int);

Thanks to Barry Schwarz for catching the error.

Now i want to recheck my understanding; func_ptr is a new name to a
pointer to a function, that returns an int and takes two ints for
arguments.
 
K

Keith Thompson

JosephKK said:
Keith Thompson said:
Finally, you can declare a typedef either for the function type itself
or for a pointer to the function type:

typedef int func(int, int);

typedef int *func_ptr(int, int);
[...]

Correction: the second should be

typedef int (*func_ptr)(int, int);

Thanks to Barry Schwarz for catching the error.

Now i want to recheck my understanding; func_ptr is a new name to a
pointer to a function, that returns an int and takes two ints for
arguments.

To be precise, func_ptr is a new name for particular
pointer-to-function *type*. That type can be described in C as
"int(*)(int, int)", or in English as "pointer to function with two int
parameters returning int".
 
R

Richard Bos

Michael Melanson said:
Two guys named Curry and Howard might have a few things to say about
that. :)

Specifically, Curry-Howard correspondence says that, for a
sufficiently detailed type system, there's a one-to-one matching between
formulae (i.e, functions) and types.

In C this definitely isn't the case, but I thought I'd be pedantic. :)

For one thing, if it were the case in C, it would make qsort() an
interesting proposition.

Richard
 
R

Richard Bos

pete said:
Also, there's a lot of library functions which have the same type,
but do different things.

True, but those would, in that hypothetical C-H C, simply have different
types. One would have a type of "function taking an int, doing this with
it, and returning an int"; the other of "function taking an int, doing
_that_ with it, and returning an int". Both could receive the same int,
and the result, still being an int, could be assigned to the same int.

The problem with qsort() is that there would be no straightforward way
of declaring its fourth parameter. There would be no way to state that
qsort() takes "pointer to function taking two void *s, and returning
int". It would have to be "pointer to function taking two void *s,
comparing what they point at in exactly this way and no other, and
returning an int". You could not use the same qsort() to sort both an
array of ints and an array of strings. This would make having a Standard
qsort() function pointless.

Richard
 
D

David Thompson

Fred <[email protected]> writes:

Which is illegal. You can't legally have a function that returns a
function. You *can* have a function that returns a function pointer.
Yes.


Again, this is illegal, since you can't have a parameter of function
type. You can have a parameter of pointer-to-function type, so this
would be legal (and is probably what you meant):
No. You can't actually have a function parameter, but if you write a
parameter with a function type F, it is 'adjusted' to F*, in the same
way as the much more common and familiar case that an array type T[]
is adjusted to T* . 6.7.5.3p8 previously misplaced in 6.7.1.

In both cases, many people feel it is _clearer_ to explicitly write
the pointer type, but it is perfectly legal to do the others.

Given that func is a typedef for a function type, you declare
a function of that type:

func foo;
s/you/you can/
But you can't use it to *define* the same function:

func foo { return 42 }; /* ILLEGAL */
Yes.

In my opinion, it's clearer *not* to use the typedef in the function
declaration. (It's perfectly ok to use a typedef as *part of* a
function declaration, such as "func *bar(void)".) I prefer to keep a
function's declaration and definition as similar as possible, ...

I agree (both result and reason).
 
K

Kenny McCormack

Errr... is this a case of one Thompson trying to out-Thompson
the other Thompson? :) I'm failing to see what that change adds
to Keith's comment that wasn't already clearly implicit in it.

To be fair, I think that this was just a case of over-exuberance.

But to understand that comment, we must first examine the real,
underlying question - which is: Do newbies benefit from having these
"human compilers" go over their code line-by-line? And, the closely
related question: Do they appreciate it?

Obviously, the "human compilers" (The Thompson Twins, the Ambuhls, the
MacIntires) assume the answers to these questions are (unqualified) yes(s).
Equally clearly, sensible people aren't so sure, and some, including
this author, think the correct answer is in the negative.

Given the above, I'm sure Human Compiler David Thompson saw the above as
simply another syntax error, emitted the required diagnostic, and
moved on to the next, without giving the matter a second thought.

As I say, in that context, really just a case of over-exuberance.
 
K

Keith Thompson

David Thompson said:
<snip: typedef for function type> [...]
Again, this is illegal, since you can't have a parameter of function
type. You can have a parameter of pointer-to-function type, so this
would be legal (and is probably what you meant):
No. You can't actually have a function parameter, but if you write a
parameter with a function type F, it is 'adjusted' to F*, in the same
way as the much more common and familiar case that an array type T[]
is adjusted to T* . 6.7.5.3p8 previously misplaced in 6.7.1.

You're right, thanks for the correction.
In both cases, many people feel it is _clearer_ to explicitly write
the pointer type,

I do -- not least because a lot of C programmers don't know about the
rule.
but it is perfectly legal to do the others.

[...]
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top