简体   繁体   English

在C printf中无效使用void表达式

[英]invalid use of void expression in c printf

I am studying c. 我正在学习c。 I have this struc: 我有这个结构:

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

In main I call: 我主要是这样称呼的:

MyStuff fruit1 ={"watermellon", 1};

I pass data to insertLifo: 我将数据传递给insertLifo:

insertLifo(myQueue, &fruit1);

This is the function 这是功能

bool insertLifo(LifoQueue queue, void* data)

Inside the function I want to print the data in order to debug: 在函数内部,我想打印数据以进行调试:

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

I get the following error: 我收到以下错误:

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);
  ^

I have tried: 我努力了:

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

and other ways. 和其他方式。

I want to understand fully how to use pointers 我想充分了解如何使用指针

A void * is a generic pointer. void *是通用指针。 The type of the data it points to is unknown, therefore you can't dereference a void * . 它指向的数据类型是未知的,因此您不能取消引用void *

You should change your function to accept a MyStuff * and modify the printf call to print each field. 您应该更改函数以接受MyStuff *并修改printf调用以打印每个字段。

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

Your mistake is in how you declare insertLifo() , as you declare incorrectly the second parameter. 您的错误在于如何声明insertLifo() ,因为您错误地声明了第二个参数。 It should be: 它应该是:

bool insertLifo(LifoQueue queue, MyStuff* data)

so you can pass the variable by reference. 因此您可以通过引用传递变量。 Then, you have to do: 然后,您必须执行以下操作:

printf("bla bla %s", data->name);

as the %s format specifier requires to pass a parameter of type char * , as name is. %s格式说明符要求按name原样传递char *类型的参数。

But, as I told you in a comment to your question, we need a complete and verifiable example to be able to make a diagnostic. 但是,正如我在对您的问题的评论中告诉您的那样,我们需要一个完整且可验证的示例来进行诊断。 You didn't do this, so you get incomplete responses. 您没有执行此操作,因此您得到的响应不完整。 Please read How to create a Minimal, Complete, and Verifiable example 请阅读如何创建最小,完整和可验证的示例

Another mistake is that probably you need to pass also the first parameter of insertLifo() by reference, as you'll need probably to modify that structure, and if you pass it by value, it will be copied into the function, and any modification you do to it will be lost at function return. 另一个错误是,您可能还需要通过引用传递insertLifo()的第一个参数,因为您可能需要修改该结构,并且如果按值传递它,它将被复制到函数中,并进行任何修改您对它所做的操作将在函数返回时丢失。

Finally, you say 最后,你说

I want to understand fully how to use pointers. 我想充分理解如何使用指针。

Well, that's completely out of the scope of this application. 好吧,这完全超出了此应用程序的范围。 StackOverflow is only to help you to correct a small problem in your code, but not to teach you how to program. StackOverflow只是为了帮助您纠正代码中的一个小问题,而不是教您如何编程。 I can say that many people pass all they lifes trying to understand how to use pointers and never succeed. 我可以说,许多人毕生尝试了解如何使用指针,但从未成功。 That depends on many aspects, but a good thing you can do is to buy a good book on C. 这取决于很多方面,但是您可以做的一件好事是购买一本有关C的好书。

In order to print pointer address you shell use %p printf format instead of %s which is for the string. 为了打印指针地址,您在外壳程序中使用%p printf格式,而不是用于字符串的%s

If you need a to string printf you can use following: 如果需要将printf字符串化,则可以使用以下命令:

MyStuff *stuff = (MyStuff*)data;
printf("insertLifo() data=%p = {name=%s, val=%d} \n", data, stuff->name, stuff->val);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM