尝试使用函数指针时出现“错误:无效使用无效表达式”

时间:2019-10-20 11:54:10

标签: c compiler-errors c89

我有两个功能:

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

void B(void* string)

总的来说,我像这样调用函数A;

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

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

但是,当我尝试编译程序时,在调用函数A时出现错误。我可以确定我没有正确使用函数指针。有人可以给我一些指导吗?

1 个答案:

答案 0 :(得分:1)

您的意图可能是:


#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;
}