Error with function pointers

I use this resource to help me with function pointers: here But in this code (see below), gcc compilation says:

line 15: warning: dereferencing 'void*' pointer
line15:error:called object *foo is not a function

The code is here:

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

void print_mess(void *ptr)
{
        char *message = ptr;
        printf("%s\n",message);
}
void main()
{
        void* foo = print_mess;
        char *mess = "Hello World";
        (*foo)((void*)mess);
}

A very simple test function to refresh my knowledge, and I'm confused to even run into such a problem, not to mention its placement on SO.

+3
source share
1 answer

The pointer is incorrect. You need to use:

void (*foo)(void *) = print_mess;

It looks weird, but it's a function pointer definition. You can also print it:

typedef void (*vp_func)(void *);
vp_func foo = print_mess;
+4
source

Source: https://habr.com/ru/post/1791399/


All Articles