兼容的指针类型 - 为什么? [英] incompatible pointer type - why?

查看:877
本文介绍了兼容的指针类型 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的<一个内部工作href=\"http://www.freebsd.org/cgi/man.cgi?query=queue&apropos=0&sektion=0&manpath=FreeBSD%209.0-RELEASE&arch=default&format=html\"相对=nofollow>队列(3)在FreeBSD 宏。我曾问一个previous 问题关于同一主题的,这是给它一个跟进的问题。

I am trying to understand the inner workings of queue (3) macros in Freebsd. I had asked a previous question about the same topic and this is a follow up question to it.

我试图定义一个函数来一个元素插入到队列中。 <一href=\"http://www.freebsd.org/cgi/man.cgi?query=queue&apropos=0&sektion=0&manpath=FreeBSD%209.0-RELEASE&arch=default&format=html\"相对=nofollow>队列(3)提供宏观 STAILQ_INSERT_HEAD 这需要一个指向队列的头,队列和项目类型要插入项。我的问题是,我得到

I am trying to define a function to insert an element into the queue. queue (3) provides the macro STAILQ_INSERT_HEAD which needs a pointer to the head of the queue, type of the items in queue and the item to be inserted. My problem is that I am getting

stailq.c:31: warning: passing argument 1 of 'addelement' from incompatible pointer type

错误,当我尝试的地址传递给函数。完整的源$ C ​​$ c是如下:

error when I try to pass the address of head to the function. The full source code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>

struct stailq_entry {
        int value;
        STAILQ_ENTRY(stailq_entry) entries;
};

STAILQ_HEAD(stailhead, stailq_entry);

int addelement(struct stailhead *h1, int e){
        struct stailq_entry *n1;
        n1 = malloc(sizeof(struct stailq_entry));
        n1->value = e;
        STAILQ_INSERT_HEAD(h1, n1, entries);
        return (0);
}
int main(void)
{
        STAILQ_HEAD(stailhead, stailq_entry) head = STAILQ_HEAD_INITIALIZER(head);
        struct stailq_entry *n1;
        unsigned i;
        STAILQ_INIT(&head);                     /* Initialize the queue. */

        for (i=0;i<10;i++){
                addelement(&head, i);
        }
        n1 = NULL;

        while (!STAILQ_EMPTY(&head)) {
                n1 = STAILQ_LAST(&head, stailq_entry, entries);
                STAILQ_REMOVE(&head, n1, stailq_entry, entries);
                printf ("n2: %d\n", n1->value);
                free(n1);
        }

        return (0);
}

据我所知,的类型为结构stailhead addElement方法函数也需要一个指向结构stailhead

As far as I can tell, head is of type struct stailhead and the addelement function also expects a pointer to struct stailhead.

STAILQ_HEAD(stailhead,stailq_entry); 扩展为:

struct stailhead {
  struct stailq_entry *stqh_first;
  struct stailq_entry **stqh_last; 
};

我是什么在这里失踪?

What am I missing here?

感谢。

推荐答案

您只需要在第一线转换成你的函数从

You just need to convert the first line in your main function from

STAILQ_HEAD(stailhead, stailq_entry) head = STAILQ_HEAD_INITIALIZER(head);

struct stailhead head = STAILQ_HEAD_INITIALIZER(head);

这是怎么回事的是, STAILQ_HEAD 是一个宏,定义一个新的类型,结构就是你的数据结构的第一个参数与项类型的名称第二个参数。

What's happening is that STAILQ_HEAD is a macro that defines a new type, a struct that is your data structure with the name of the first parameter with an entry type of the second parameter.

你只应该叫 STAILQ_HEAD 一旦定义的键入的该结构 - 然后使用从上面类型名来创建新这种类型的数据结构

You're only supposed to call STAILQ_HEAD once to define the type of the struct - then you use that typename from thereon to create new data structures of this type.

什么您在code样品中确实很简单:您定义的命名结构 stailhead 两次 - 在全球范围内一次一次的范围的功能。那么你是一个指针传递给当地的 stailhead 来接受了全局类型具有相同名称的函数。

What you did in your code sample is simple: you defined a struct named stailhead twice - once in the global scope and once in the scope of your main function. You were then passing a pointer to the local stailhead to a function that accepted the global type with the same name.

尽管两个结构是相同的,他们在两个不同的存储范围和编译器会将它们视为不同的类型。它警告你,你是从类型转换主要:: stailhead 键入全球:: stailhead (注意,我刚才提出了这个符号,我不相信这是佳能)。

Even though both structs are identical, they're in two different storage scopes and the compiler treats them as distinct types. It's warning you that you're converting from type main::stailhead to type global::stailhead (note that I have just made up this notation, I do not believe it is canon).

您只需要通过在文件的顶部调用 STAILQ_HEAD 宏只是一次定义 stailhead ,你已经做了,并且由此使用结构stailhead 来定义这种类型的对象。

You only need to define stailhead by calling the STAILQ_HEAD macro just once at the top of the file where you already did, and from thereon use struct stailhead to define an object of this type.

这篇关于兼容的指针类型 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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