3

I am using this resource to help me out with Function Pointers: here But in this code (written below), compilation on gcc 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);
}

Very simple test function to brush up my knowledge, and I am embarassed to even encounter such a problem, let alone post it on SO.

1 Answer 1

4

Your pointer is the wrong type. You need to use:

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

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

typedef void (*vp_func)(void *);
vp_func foo = print_mess;
Sign up to request clarification or add additional context in comments.

2 Comments

You mean vp_func = print_mess in the second line of the second code sample? What an idiot, I was to commit such a blunder!
@Soham, oops, forgot the variable name. But no, not quite :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.