Function name vs pointer to function

S

Sortie

Is a function name in C treated the same as a pointer to that
function? For example, the following program compiles and runs
fine:

#include <stdio.h>
#include <stdlib.h>

int callback(int x){return x;}

void print_callback(int cb()){
printf("%d\n", cb(10));
}

int main(int argc, char *argv[]){
print_callback(callback);

return 0;
}

If so, why should I explicitly use a pointer to a function?
 
B

Ben Pfaff

Sortie said:
Is a function name in C treated the same as a pointer to that
function?

Yes, but...
void print_callback(int cb()){
printf("%d\n", cb(10));
}

....this is not an example of using a function name as a pointer
to a function, since there is no function name here, only a
parameter declared to have function type. This is actually a
special case, which in C99 (and similarly in C89) is explicitly
described in the standard:

A declaration of a parameter as ``function returning type''
shall be adjusted to ``pointer to function returning
type''...
int main(int argc, char *argv[]){
print_callback(callback);

This is indeed an example: "callback" is the name of a function,
which is treated as a pointer to that function.
return 0;
}

If so, why should I explicitly use a pointer to a function?

You can only take advantage of this special case in the
declaration of a function. If you want to declare a function
pointer other than a function parameter, you need to use the
normal system for pointer-to-funtion.
 
B

Ben Bacarisse

Sortie said:
Is a function name in C treated the same as a pointer to that
function?

In most contexts, yes. The exceptions are sizeof func_name and
&func_name. The first is a constraint violation (roughly speaking a
compiler-time error) and the second gives you the pointer you'd get if
missed off the & anyway.
For example, the following program compiles and runs
fine:

#include <stdio.h>
#include <stdlib.h>

int callback(int x){return x;}

void print_callback(int cb()){

This is a special case. Just as array types in function argument
lists get adjusted to pointer types, so do function types. I.e. what
you wrote is shorthand for

void print_callback(int (*cb)()){...}
printf("%d\n", cb(10));

You'd get better checking if you give the parameter types of the
call-back.
}

int main(int argc, char *argv[]){
print_callback(callback);

return 0;
}

If so, why should I explicitly use a pointer to a function?

In some ways you are -- it depends on what counts as explicit to you.
Do you mean why would one write &func_name? Some people might
consider it clearer, but I would not bother. As to why would
one need to write explicit function pointer *types*, then you would
need to everywhere except for function parameters where the special
rule means you don't need to. Personally, I'd put the "adjusted" type
there myself -- i.e. I would not reply on this special rule.
 

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,771
Messages
2,569,587
Members
45,099
Latest member
AmbrosePri
Top