dingodingo
dingodingo

Reputation: 3

Getting "error: invalid use of void expression" when trying to use a function pointer

I have two functions:

void A (void(*fptr)(void*))

void B(void* string)

In main, I am calling function A like so;

char* bird = (char*)malloc(sizeof(char)*100)
strcpy(bird, "bird");

A((*B)(bird)); //error: invalid use of void expression

However, when I try to compile the program, I get an error when calling function A. I'm pretty certain that I am not using the function pointer correctly. Can somebody provide me some guidance?

Upvotes: 0

Views: 145

Answers (1)

wildplasser
wildplasser

Reputation: 44250

Probably, your intention is:


#include <stdlib.h>
#include <string.h>

void A(void(*fptr)(void*), void *ptr); // two arguments for A()
void B(void* string);


int main(void)
{
char *bird = malloc(100);
strcpy(bird, "bird");

A(&B, bird);  // OR: A(B, bird); which is the same
return 0;
}

Upvotes: 1

Related Questions