【问题标题】:Getting "error: invalid use of void expression" when trying to use a function pointer尝试使用函数指针时出现“错误:无效使用无效表达式”
【发布时间】:2019-10-20 11:54:10
【问题描述】:

我有两个功能:

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

void B(void* string)

在main中,我这样调用函数A;

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

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

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

【问题讨论】:

  • 你期望*B 是什么?
  • 指向函数的指针
  • A() 只接受一个参数(指向函数的指针)。 *B 不是指向函数的指针。

标签: c compiler-errors c89


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多