The code states:
void (* log_msg)(char *msg)
=printf;
void change_and_log(int *buffer, int offset, int value){
buffer[offset] = value;
log_msg("changed");
}
I'm most concerned with the first part:
Firstly, what does the signature void (* log_msg)(char *msg) mean? Is this code simply mapping the function log_msg to printf? In that case, why is the function name (* log_msg) and not simply log_msg?
void (* log_msg)(char *msg) is actually a function pointer. You could view this as
typedef void (*LoggerFunctionPointer)(char* msg);
LoggerFunctionPointer log_msg = printf;
Yes, it maps log_msg to printf, but no, log_msg isn't a function, but a pointer that points to a function printf.
Using a function pointer has the advantage that the log_msg can be switched at runtime. For example, you could provide a switch in the interface that
void no_log_msg(char* msg) {}
...
if (enable_debug) {
log_msg = printf;
} else {
log_msg = no_log_msg;
}
then, without changing other source code, all logging can be inhibited.
(BTW, the sample code is incorrect because the signature of printf is int printf(const char*, ...). To avoid the implicit cast, log_msg should be declared as
int (*log_msg)(const char*, ...) = printf;
)
It's a function pointer.
The type of a function pointer is R (*)(Args...), where R and Args... are replaced with the return type and arguments, if any. It is read as "a pointer to a function that takes arguments Args... and returns R."
Your code would read easier as:
// print_function is a type that is a function pointer
typedef void (*print_function)(char *msg);
// log_msg is a variable of the type print_function: it points to a function
print_function log_msg = printf; // point to printf
And later, it's just calling that function via a function pointer.
The top two lines are establishing a function pointer (hence the *), the function is named log_msg, and it's then set to point to printf - after which a call to log_msg ends up calling printf.
log_msg is a function pointer, in particular a "pointer to a function taking a char * and returning void". In this case it's just used as an alias for printf (but could point to any function with the same arguments and return type).
The syntax for function pointers in C can be pretty intimidating at first.
Related
I would be very thankful if you could explain me what the following means:
void bar(char *a, char *b, unsigned short c) // (2)
{
...
}
void (*foo(void))(char *, char *, unsigned short) // (1)
{
return bar;
}
In particular,
Why there are no variable names in (1)?
What does void (*foo(void)) mean? How can *foo(void) be a name?
What does return bar mean? Returns it the address of the bar source code, or the result from bar, or else?
Is there any feature in making these signatures so complicated?
Could you give an example of the usage?
foo is a function that takes no arguments and returns a pointer to a function that takes three arguments of types char *, char * and unsigned short and returns void.
These declarations can be quite confusing because they should be read inside-out, bouncing left and right as needed:
foo is a thing ...
foo(void) ... apparently a function
*foo(void) ... whose return value can be dereferenced
(*foo(void))(...) ... and then called with these arguments
void (*foo(void))(...) ... which results in a value of type void.
You can also use cdecl.org to parse complex declarations for you:
declare foo as function (void) returning pointer to function (pointer to char, pointer to char, unsigned short) returning void
Usage examples:
// Immediately call the returned function.
// The similarity to the declaration is no coincidence!
(*foo())("hello", "world", 42);
// Store the returned function pointer for later invocation.
// This time there are no parentheses following the name, because
// this is a variable, not a function.
void (*fnPtr)(char *, char *, unsigned short) = foo();
(*fnPtr)("hello", "world", 42);
Argument names can always be omitted if the arguments aren't used inside the function. In this case, there isn't even a function body to use them in, because the body of foo isn't what the arguments are being passed to.
I was reading this code on a website. I am fairly new to programming so please explain in a bit more detail.
#include <stdio.h>
// A normal function with an int parameter
// and void return type
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;
/* The above line is equivalent of following two
void (*fun_ptr)(int);
fun_ptr = &fun;
*/
// Invoking fun() using fun_ptr
(*fun_ptr)(10);
return 0;
}
Doubts-
I am not able to understand this type of declaration and assignment void (*fun_ptr)(int) = &fun;
I mean that if we declare a data type, then we do it like int a; and assign it as a=10; but here we are assigning it by writing (*fun_ptr)(10);. Kindly help.
Instead of this record
(*fun_ptr)(10);
you could just write
fun_ptr(10);
That is it is a function call of the function fun pointed to by the function pointer fun_ptr due to the initialization of that pointer in its declaration by the function address
void (*fun_ptr)(int) = &fun;
In turn this declaration could be written simpler like
void (*fun_ptr)(int) = fun;
because a function designator (in this case fun) used in expressions as for example an initializer is implicitly converted to pointer to the function.
You could use a typedef alias for the function type the following way
typedef void Func( int );
In this case the above declaration of the function pointer could look simpler like
Func *fun_ptr = fun;
Here is your program rewritten using a typedef for the function type of the function fun.
#include <stdio.h>
typedef void Func( int );
// Function declaration without its definition using the typedef
// This declaration is redundant and used only to demonstrate
// how a function can be declared using a typedef name
Func fun;
// Function definition. In this case you may not use the typedef name
void fun( int a )
{
printf("Value of a is %d\n", a);
}
int main(void)
{
// Declaration of a pointer to function
Func *fun_ptr = fun;
// Call of a function using a pointer to it
fun_ptr( 10 );
return 0;
}
Lets rewrite it a little bit, by using type-aliases and some comments:
// Define a type-alias names fun_pointer_type
// This type-alias is defined as a pointer (with the asterisk *) to a function,
// the function takes one int argument and returns no value (void)
typedef void (*fun_pointer_type)(int);
// Use the type-alias to define a variable, and initialize the variable
// This defines the variable fun_ptr being the type fun_pointer_type
// I.e. fun_ptr is a pointer to a function
// Initialize it to make it point to the function fun
fun_pointer_type fun_ptr = &fun;
// Now *call* the function using the function pointer
// First dereference the pointer, to get the function it points to
// Then call the function, passing the single argument 10
(*fun_ptr)(10);
Hopefully it makes things a little clearer what's going on.
Following is the meaning of two statments.
void (*fun_ptr)(int) = &fun; this is called declaring and initializing the fun_ptr in the same line , this is same as doing int a = 10;
(*fun_ptr)(10); is not assignment statement, it is invoking the function fun through function pointer fun_ptr.
you can also use typedef to create a new user defined out of function pointer and use as shown in above answer.
This is an advanced topic if you are new to programming, fun_ptr is a pointer to a function.
The declaration:
void (*fun_ptr)(int) = fun;
Means fun_ptr is a pointer to a function taking an int argument and returning void, initialize if with a pointer to the function fun. (you don't need the &)
The line:
(*fun_ptr)(10);
does not assign anything, it calls the function pointed to by fun_ptr, but is way to complex
fun_ptr(10);
As fun_ptr points to fun this is equivalant to `fun(10).
Using a function pointer has its use eg in a sort function where the comparison function is passed in as a function pointer so the sorting can be different between calls.
achieves the same thing and is much easier on the eyes.
I have come across the line of code shown below.
I think it may be a cast to a function pointer that returns void and takes a void pointer. Is that correct?
(void (*)(void *))SGENT_1_calc
Yes, it is correct. I find that not very readable, so I suggest declaring the signature of the function to be pointed:
typedef void sigrout_t(void*);
I also have the coding convention that types ending with rout_t are such types for functions signatures. You might name it otherwise, since _t is a suffix reserved by POSIX.
Later on I am casting, perhaps to call it like
((sigrout_t*) SGENT_1_calc) (someptr);
Yes, it is. The function should be looking like this
void func(void*);
But the statement is missing a target, since a cast to nothing is useless. So it should be like
func = (void (*)(void *))SGENT_1_calc;
None of the existing answers show it in direct usage, that is, taking a function pointer and casting it in order to call the function. I was playing with this to show the content of my object as json, accessing both the function and the data through anonymous pointers:
#include <stdio.h>
#include <stdlib.h>
typedef struct box1_s{
int a;
char b[50];
}box1_t;
void box1_t_print(void* ptr){
box1_t* box = (box1_t*)ptr;
printf("{\"a\": %i, \"b\": \"%s\"}", box->a, box->b);
}
int main(){
void* print = (void*)box1_t_print;
box1_t mybox = {3, "Hi folks, it's me!"};
void* ptr = &mybox;
printf("mybox = ");
((void (*)(void*))print)(ptr);
return 0;
}
Output of the program:
mybox = {"a": 3, "b": "Hi folks, it's me!"}
Yes, this is a function pointer cast.
Function pointer casts
To help you with casting functions to pointers, you can define an alias for a function pointer type as follows:
typedef void void_to_void_fct(void*);
You can also define a type for a function that takes and returns values:
typedef int math_operator(int, int);
Later, you can store a function into a function pointer type like this:
void mystery(void* arg) {
// do something nasty with the given argument
};
int add(int a, int b) {
return a + b;
}
void_to_void *ptr1 = mystery;
math_operator *ptr2 = add;
Sometimes, you have a function like print_str :
void print_str(char* str) {
printf("%s", str);
}
and you want to store it in your function pointer that is agnostic to the argument type. You can then use a cast like this:
(void (*)(void *))print_str
or
(void_to_void_fct*)print_str
Why do we use function pointers?
Function pointers allow you to "store a function" inside a variable (indeed, you store the address of the function). This is very convenient when you want to allow some code to have diferent behavior depending on user input.
For exemple, suppose we have some data and some way to decode it. We could have the following structure to keep this information:
typedef char* decoder_type(char*);
struct encoded_data {
char* data;
decoder_type *decoder_fct;
};
char* decoding_function_1(char* data) {
//...
char* decoding_function_2(char* data) {
//...
This allows storing both the data and the function to later use them together to decode the data.
I am confused with function pointer declaration.
I have an api abc() which takes an argument as so:
void abc(void (*my_func)(void *p), int, int)
If I want to pass my function as an argument to that api, I am declaring it in my .h file:
void (*xyz)(void *p)
and defining as:
void *(xyz)(void *p){
statements;
}
but this throws an error. Please correct me.
you just need to declare it:
void xyz(void *p);
with the implementation the same way.
When you pass it into your api, the type system figures out it out automatically:
abc(xyz,someint,anotherint);
The (*xyz) means that it is a function pointer.
Function pointers are best handled with typedefs. So it is guaranteed that there is nothing wrong.
I would do the following:
// define a type for the function (not its pointers, as you can often read)
typedef void my_func_t(void *p);
void abc(my_func_t*, int, int);
// declaration in order to be type-safe - impossible if only the pointer would be typedef'd
my_func_t my_func_impl;
// definition:
void my_func_impl(void *p)
{
do_something_with(p);
}
and then you can call your abc() with abc(my_func_impl, 47, 11). You can put a & before my_func_impl there in order to point out that it is the function address you wish to obtain, but it is optional.
An alternative would be to write
typedef void (*my_func_p)(void *p);
and use my_func_p instead of my_func_t *, but this has the disadvantage that you cannot write my_func_t my_func_impl;.
Why would you want to do that?
Well, if, by any coincidence or accident, the function definition or the typedef is changed, they won't match any longer, but the collision is not declared as error, but only as warning (Mismatch pointer). OTOH, my_func_t my_func_impl; serves as a kind of prototype, which causes a function header mismatch, which is an error.
Simply declare and define your function as you would any other:
void xyz(void *p);
void xyz(void *p){
// ...
}
and pass a pointer to the API:
abc(xyz, 42, 7);
Function names are automatically interpreted as function pointers where appropriate. You can also explicitly take the address of the function, if brevity isn't your thing:
abc(&xyz, 42, 7);
If I understood correctly, you want xyz to be the function that is passed to abc, right?
As the argument my_func indicates, you have a pointer to a function that takes void * as argument and returns void
type (*func_pointer)(type, type, type, .......)
^ ^ ^ ^ ^ ^
| | | | | |
| | | argument types
| | pointer name
| This is a function pointer
return type
Therefore, you need to declare xyz as:
void xyz(void *p);
And the same in implementation:
void xyz(void *p){
statements;
}
What you are doing wrong is that, the line you wrote in the .h file defines a function pointer, named xyz. The function pointer has no value, because you never wrote xyz = some_function;
What you have written in the source file is a function, also with name xyz that takes a void * as input and returns a void *, instead of void which was your intention.
Maybe this helps you get less confused:
When you write int *x;, x is a pointer to int. Then you can have int y; that doesn't have an extra * and write x = &y;.
It's the same with functions. If you have void (*funcptr)(void *p);, then all you need is a function that says void some_func(void *p){} (again without the extra *) and write funcptr = some_func;. You don't need the & since function names are in fact pointer to the function. You could put it to be more explicit though.
The first argument of 'abc' is the pointer of function returning 'void' and having 'void *' as an argument...
So your code should look like:
void
myFunc (void *)
{
// ... my statements
}
...
abc (myFunc, 10, 20);
This works
void abc(void* (*my_func)(void*), int a, int b) {
my_func(0);
}
void *(xyz)(void *p) {}
int main() {
abc(xyz, 0, 0);
return 0;
}
When you write void (*my_func)(void *p) it means pointer to function, that returns void
And void (*my_func)(void *p) it means pointer to function, that returns pointer
Please tell me what will the call to given function return and how? The code:
typedef struct {
int size;
ptrdiff_t index;
void (*inlet) ();
int argsize;
ptrdiff_t argindex;
} CilkProcInfo;
/*
* Returns a pointer to the slow version for a procedure
* whose signature is p.
*/
/* the function definition is - */
static void (*get_proc_slow(CilkProcInfo *p)) () {
return p[0].inlet;
}
/*The function gets called as -*/
(get_proc_slow(f->sig)) (ws, f);
/*where f->sig is a pointer to CilkProcInfo struct*/
In your CilkProcInfo structure, inlet is a pointer to a function that takes an unspecified number of arguments and does not return a value, like void foo();.
In the line
(get_proc_slow(f->sig)) (ws, f);
the get_proc_slow(f->sig) call returns this function pointer, so it is equivalent to
(f->sig[0].inlet) (ws, f);
So if your f->sig[0].inlet points to the function foo(), it is equivalent to the call
foo (ws, f);
I should admit that the static void (*get_proc_slow(CilkProcInfo *p)) () {... syntax is a bit unfamiliar to me.
get_proc_slow() returns a function pointer of type void(*)() which the code then calls. So when you do:
(get_proc_slow(f->sig)) (ws, f);
It's basically same as doing:
void (*fptr)() = get_proc_slow(f->sig);
fptr(ws, f);
It looks like it's a function that returns a pointer to a function whose return value is void that has no parameters (void(*)()) and that accepts a pointer to a CilkProcInfo struct as a parameter. I'm not sure why you'd need the p[0].inlet construct though. Couldn't you just return it as p->inlet?
Oh yeah, and get_proc_slow is the name of the function that returns said function pointer.
static void (*get_proc_slow(CilkProcInfo *p)) () {
return p[0].inlet;
}
Reading from the name out, taking care with the grammar rules: get_proc_slow is a function (with internal linkage) that takes a pointer to a CilkProcInfo struct and returns a pointer to a function taking unspecified arguments and returning no value (void).
(get_proc_slow(f->sig)) (ws, f);
This statement calls the get_proc_slow with an appropriate parameter (f->sig is a pointer to a CilkProcInfo) and then uses the return value (a pointer to a function) to call that function with ws and f as arguments.