gpt4 book ai didi

c - 尝试使用函数指针时获取 "error: invalid use of void expression"

转载 作者:行者123 更新时间:2023-11-30 14:35:31 34 4
gpt4 key购买 nike

我有两个功能:

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 时出现错误。我非常确定我没有正确使用函数指针。有人可以给我一些指导吗?

最佳答案

您的意图可能是:

<小时/>
#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;
}

关于c - 尝试使用函数指针时获取 "error: invalid use of void expression",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58472742/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com