简体   繁体   English

结构中的指针“ addr”被分配了内存,但是当我在另一个函数中调用它时,它将无法工作

[英]A pointer “addr” in a struct was allocated memory but when I call it in another function, it can't work

At the beginning of the C file, I define two structures like this. 在C文件的开头,我定义了两个这样的结构。

typedef struct
{
    char name[LEN];
    double price;
} book;

typedef struct
{
    void *addr;
    int num;
} Array;

Array book;

Later, I use malloc(sizeof(void *)*len) to allocate memory to the pointer addr . 稍后,我使用malloc(sizeof(void *)*len)将内存分配给指针addr The void pointer points to a book structure which is also allocated memory in heap. void指针指向一个书结构,该书结构也在堆中分配了内存。

After doing this, when I call it in another function in this way: 完成此操作后,当我以这种方式在另一个函数中调用它时:

void print_view_of_books(Array books)
{
   int j;
   int limit = books.num;
   for(j=0; j<limit; j++)
   {
      book * bk = (book *)books.addr[j]; 
      printf("Book title: %s\n", bk->name);
      printf("Book price: %lf\n", bk->price);
      puts("----------------------");
   }

}

My compiler says 我的编译器说

error: operand of type 'void' where arithmetic or pointer type is required. 错误:类型为“ void”的操作数,其中需要算术或指针类型。 1 error generated. 产生1个错误。

I find there's a problem with the expression books.addr[j] and if I substitute it simply with books.addr , it will work. 我发现表达式books.addr[j]存在问题,如果我将它简单地替换为books.addr ,它将起作用。

Can somebody instruct me how to fix the problem please? 有人可以指导我如何解决问题吗?

This is a problem of operator precedence. 这是运算符优先级的问题。

Changing book * bk = (book *)books.addr[j]; 更改book * bk = (book *)books.addr[j]; to book * bk = ((book *)books.addr)[j]; book * bk = ((book *)books.addr)[j]; should fix the problem. 应该解决问题。

error: operand of type 'void' where arithmetic or pointer type is required. 错误:类型为“ void”的操作数,其中需要算术或指针类型。 1 error generated. 产生1个错误。

You are using pointer arithmetic on a void pointer which is not allowed ( addr[ j ] ). 您正在不允许的void指针(addr [j])上使用指针算术。

First you have to cast the void pointer 首先,您必须强制转换void指针

((book *)(books.addr))

And then dereference it 然后取消引用

((book *)(books.addr))[j]

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

相关问题 我可以使用临时地址结构调用 bind() 吗? - Can I call bind() with a temp addr struct? 我可以释放最初从另一个指针分配的内存吗? - Can I free memory originally allocated from another pointer? 将结构成员指针分配给另一个动态内存分配指针是否安全? - Is it safe to assign a struct member pointer to another dynamically memory allocated pointer? 我可以将指向结构的指针的本地实例添加到用 malloc 分配的内存到结构的全局数组和 free() 正确吗 - Can I add a local instance of a pointer to a struct with memory allocated with malloc to a global array of structs and free() properly 无法将字符从指针复制到另一个指针(两者都分配了内存) - Can't copy characters from pointer to another pointer(both with memory allocated) 在 void function 调用上分配指针字段的结构 - Struct with pointer fields allocated over void function call 除非先前已分配(并释放),否则无法引用指向函数的结构指针 - can't reference a struct pointer to function unless it has been previously allocated (and freed) 将内存分配给函数(定义或调用)时 - When memory is allocated to a function (definition or call) C:分配给结构的空闲内存不起作用 - C: free memory allocated to a struct doesn't work 为什么我不能从C中返回的结构调用函数指针? - Why can't I call function pointer from returned struct in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM