gpt4 book ai didi

c - 在 c printf 中无效使用 void 表达式

转载 作者:太空宇宙 更新时间:2023-11-04 05:22:38 25 4
gpt4 key购买 nike

我正在学习c。我有这个结构:

typedef struct mystuff
{
char* name;
int val;
}MyStuff;

在主要我调用:

MyStuff fruit1 ={"watermellon", 1};

我将数据传递给 insertLifo:

insertLifo(myQueue, &fruit1);

这是函数

bool insertLifo(LifoQueue queue, void* data)

在函数内部我想打印数据以便调试:

printf("insertLifo()  %s \n", *data);

我收到以下错误:

MemAlloc.c: In function ‘insertLifo’:
MemAlloc.c:42:32: warning: dereferencing ‘void *’ pointer [enabled by default]
printf("insertLifo() %s \n", *data);
^
MemAlloc.c:42:2: error: invalid use of void expression
printf("insertLifo() %s \n", *data);
^

我试过:

printf("insertLifo()  %p \n", (void*)*data);

以及其他方式。

我想完全理解如何使用指针

最佳答案

void * 是一个通用指针。它指向的数据类型未知,因此您不能取消引用 void *

您应该更改函数以接受 MyStuff * 并修改 printf 调用以打印每个字段。

bool insertLifo(LifoQueue queue, MyStuff* data)
{
...
printf("insertLifo() name=%s val=%d \n", data->name, data->val);
...

关于c - 在 c printf 中无效使用 void 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54933564/

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