C 如何在使用 struct* = struct 时从不兼容的指针类型解析赋值 [英] C how can i resolve assignment from incompatible pointer type when using struct* = struct

查看:71
本文介绍了C 如何在使用 struct* = struct 时从不兼容的指针类型解析赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这些论坛上搜索以寻找问题的解决方案,但我似乎无法找到适合我的特定解决方案的解决方案.

I am trawled these forums a looking for a solution to the problem but i could not seem to located one for my specific solution.

我遇到的问题是,我想实现一个链表方面,但我不关心列表的头部,我只需要知道结构的顺序.

The problem i am having is, i want to implement a linked list aspect but i am not concerned about the head of the list, i just need to know the order to the structures.

总体思路是:

结构体1->结构体2->结构体n->结构体n+1

struct 1 -> struct 2-> struct n->struct n+1

所以我可以去:

结构 n -> 结构 n-1 ........

struct n -> struct n-1 ........

我已经定义了我的标题:

i have defined my header as so:

 typedef struct {

        unsigned int value;
        struct item* prev;


    } item;

我从一个简单的文件中读取了多个项目值,该文件基本上是:

I read in muliple item values from a simple file which is basically:

23
421
12
234
etc ...

我将项目加载到一个足够大的 malloc 内存区域,当我从文件中读取它们时,我这样做:

I load the items into an area of malloc'd memory large enough and when i read them in from the file i do this:

item* items;
items = malloc(no_of_lines * sizeof(item));

 for (i = 0; i < no_of_lines; i++) {
        if (fscanf(fp, "%d", &item[i] != EOF) {
      }
    }

但是当我尝试将前一个分配给一个项目时:

But when i try to assign the previous to an item:

for (i = 0; i < no_of_lines; i++){
   if(i != 0){
      item[i].prev = item[i-1];
      }
 }

我收到此错误:

incompatible types when assigning to type ‘struct item *’ from type ‘item’

我已经试过了:

for (i = 0; i < no_of_lines; i++){
       if(i != 0){
          item[i].prev = &item[i-1];
          }
     }

for (i = 0; i < no_of_lines; i++){
           if(i != 0){
              item[i].prev = (item *)&item[i-1];
              }
         }

两者都产生警告:

assignment from incompatible pointer type

有什么想法吗?

推荐答案

问题出在这个定义上:

typedef struct {
    unsigned int value;
    struct item* prev;  /* <---- here */
} item;

那行没有声明指向这个结构的指针,因为名称item直到完整声明的结尾才被引入.这实际上是一个无名结构体,具有名为 item 的别名.在某个地方,您有另一个可能类似的结构,名为 item:

That line is not declaring a pointer to this struct, because the name item is not introduced until the end of the full declaration. This is actually a nameless struct, with an alias named item. Somewhere you have another, maybe similar, struct that is named item:

struct item {
     //unknown contents (for me)
};

不同的是,这种类型被命名为struct item,而原来的只是item,没有struct关键字.

The difference is that this type is named struct item while the original one is just item, without the struct keyword.

如果它们不相同,您应该将它们命名为不同的名称,以避免混淆.如果它们实际上相同,则应删除其中一个声明.请注意,您可以同时声明命名结构和别名,然后它们都将引用相同的类型:

If they are not the same, you should name them differently, to avoid confusion. And if they are actually the same, you should remove one of the declarations. Note that you can declare the named struct and an alias at the same time, and then they both will refer to the same type:

typedef struct item {
    unsigned int value;
    struct item* prev;
} item;

或者你也可以分开声明,都是一样的;

Or you can do it in separated declarations, it's just the same;

struct item {
    unsigned int value;
    struct item* prev;
};
typedef struct item item;

除此之外,使用 & 运算符进行赋值:

Other than that, the assignment is with the & operator:

items[i].prev = &items[i-1];

这篇关于C 如何在使用 struct* = struct 时从不兼容的指针类型解析赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆