Passing pointer to function

D

David RF

Hi friends, in this example:

#include <stdio.h>

struct Demo {
char *(*fn)(const char *, const char *);
char *value;
};

static void demo_set_fn(struct Demo *demo, char *(*fn)(const char *,
const char *))
{
demo->fn = fn;
}

static void demo_set_value(struct Demo *demo, char *value)
{
demo->value = value;
}

static char *demo_print(struct Demo *demo, const char *s)
{
return demo->fn(demo->value, s);
}

static char *dummy(const char *value, const char *s)
{
static char result[50];

sprintf(result, "%s %s\n", value, s);
return result;
}

int main(void)
{
struct Demo demo;

demo_set_fn(&demo, dummy);
demo_set_value(&demo, "Hello");
printf(demo_print(&demo, "world"));
return 0;
}

what's the difference between
demo_set_fn(&demo, dummy);
and
demo_set_fn(&demo, &dummy);

is & redundant?
both are valid?
 
B

Bart

static char *dummy(const char *value, const char *s)
what's the difference between
demo_set_fn(&demo, dummy);
and
demo_set_fn(&demo, &dummy);

C insists that a function call must be followed by the arguments in
parentheses, even when there are no arguments. So the following
syntax:

dummy

which in another language could be used as a function call without
arguments, is spare. So it's used as a pointer to the function, the
same as &dummy.

Although I don't know which if any is considered the 'official' way.
 
K

Keith Thompson

Bart said:
C insists that a function call must be followed by the arguments in
parentheses, even when there are no arguments. So the following
syntax:

dummy

which in another language could be used as a function call without
arguments, is spare. So it's used as a pointer to the function, the
same as &dummy.

Although I don't know which if any is considered the 'official' way.

Given the above function declaration, the expression

dummy

is a function designator. It's actually an expression of function
type. Any expression of function type is, in most contexts,
implicitly converted to a pointer to the function; the exceptions are
``&dummy'', which yields a pointer to the function, and ``sizeof
dummy'', in which ``dummy'' remains an expression of function
type and therefore ``sizeof dummy'' is illegal. This should look
familiar; it's very similar to what happens with array expressions.
(Neither arrays nor functions are first-class objects in C; the
language goes to some effort to deal with them indirectly, via
pointers, in most contexts.)

Note that a function call actually requires a pointer to a function.
90+% of the calls you'll see just use the function name. The language
depends on the implicit conversion to make this work.

So ``dummy'', after the implicit conversion, yields a pointer to the
function.

``&dummy'' yields the same pointer without the implicit conversion.

``&&dummy'' is a constraint violation, since "&" can only be applied
to an value.

In ``*dummy'', dummy is implicitly converted to a pointer, which is
dereferenced by "*", but the resulting function value is again
implicitly converted back to the same pointer.

See also ``**********dummy''.

None if these is more "official" than the others (except for the
illegal ``&&dummy''), and a compiler is likely to generate the same
code for any of them.
 
S

Stephen Sprunk

David said:
what's the difference between
demo_set_fn(&demo, dummy);
and
demo_set_fn(&demo, &dummy);

is & redundant?
both are valid?

Both dummy and &dummy are valid, though you'll rarely see the latter. A
function designator decays into a function pointer except in the
contexts of the sizeof, (), or & operators.

S
 
K

Keith Thompson

Stephen Sprunk said:
Both dummy and &dummy are valid, though you'll rarely see the latter. A
function designator decays into a function pointer except in the
contexts of the sizeof, (), or & operators.

Correction: It's just sizeof and (unary) "&". In an ordinary function
call like
func(arg);
the expression func does decay to a pointer. Of course a compiler
needn't actually perform a conversion.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top