从 C 中不兼容的指针类型赋值

时间:2021-03-09 08:53:32

标签: c sorting struct singly-linked-list selection-sort

我有一个结构体的链表, 这是我的结构:

push_back

我想根据结构体“capacite”的元素按升序对链表进行排序。 这是函数tri的代码:

typedef struct avion
{
    int code;
    int capacite;
    char etat[1];
    int date;
    int nvols;
} avion;

typedef struct element *list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;

但我收到警告:来自不兼容指针的赋值(在 for 循环的两行中:我不知道如何修复它。

有没有更好的方法来根据这个标准对我的链表进行排序?

2 个答案:

答案 0 :(得分:0)

例如在这个 for 循环中

for (i=L; (*i)->svt != NULL; i=(*i)->svt)

变量i声明为

list *i

的类型为 struct element **。另一方面,数据成员 svt 的类型为 struct element *。因此这个任务

i=(*i)->svt

包含不同类型的操作数,没有从 struct element * 类型到 struct element ** 类型的隐式转换。

注意该函数可能会调用未定义的行为,如果因为这个表达式会调用一个空列表

(*i)->svt != NULL;

还有这个具有一个元素的数组的声明

char etat[1];

意义不大。

并为这样的指针引入这样的别名

typedef struct element *list;

总的来说不是一个好主意。它只会让代码的读者感到困惑。

没有必要通过指向头节点的指针将指向头节点的指针传递给函数tri(实现选择排序),因为指针本身在函数内没有改变。< /p>

可以通过以下方式声明和定义函数,如下面的演示程序所示。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct avion
{
    int capacite;
} avion;

typedef struct element list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;

int push_front( list **head, int capacite )
{
    element *new_element = malloc( sizeof( element ) );
    int success = new_element != NULL;
    
    if ( success )
    {
        new_element->A.capacite = capacite;
        new_element->svt = *head;
        
        *head = new_element;
    }
    
    return success;
}

void display( const list *head )
{
    for ( ; head != NULL; head = head->svt )
    {
        printf( "%d -> ", head->A.capacite );
    }
    
    puts( "null" );
}

void tri( list *head )
{
    for ( ; head != NULL; head = head->svt )
    {
        element *min = head;
        
        for ( element *current = head->svt; current != NULL; current = current->svt )
        {
            if ( current->A.capacite < min->A.capacite )
            {
                min = current;
            }
        }
        
        if ( min != head )
        {
            avion tmp = min->A;
            min->A = head->A;
            head->A = tmp;
        }
    }
}

int main(void) 
{
    enum { N = 10 };
    list *head = NULL;
    
    srand( ( unsigned int )time( NULL ) );

    for ( int i = 0; i < N; i++ )
    {
        push_front( &head, rand() % N );
    }
    
    display( head );
    
    tri( head );
    
    display( head );

    return 0;
}

程序输出可能如下所示。

7 -> 1 -> 2 -> 6 -> 0 -> 9 -> 0 -> 9 -> 6 -> 0 -> null
0 -> 0 -> 0 -> 1 -> 2 -> 6 -> 6 -> 7 -> 9 -> 9 -> null

如果使用名称 list 的别名定义,则该函数看起来就像这个演示程序中所示。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct avion
{
    int capacite;
} avion;

typedef struct element *list;
typedef struct element
{
    avion A;
    struct element *svt;
} element;

int push_front( list *head, int capacite )
{
    element *new_element = malloc( sizeof( element ) );
    int success = new_element != NULL;
    
    if ( success )
    {
        new_element->A.capacite = capacite;
        new_element->svt = *head;
        
        *head = new_element;
    }
    
    return success;
}

void display( list head )
{
    for ( ; head != NULL; head = head->svt )
    {
        printf( "%d -> ", head->A.capacite );
    }
    
    puts( "null" );
}

void tri( list head )
{
    for ( ; head != NULL; head = head->svt )
    {
        element *min = head;
        
        for ( element *current = head->svt; current != NULL; current = current->svt )
        {
            if ( current->A.capacite < min->A.capacite )
            {
                min = current;
            }
        }
        
        if ( min != head )
        {
            avion tmp = min->A;
            min->A = head->A;
            head->A = tmp;
        }
    }
}

int main(void) 
{
    enum { N = 10 };
    list head = NULL;
    
    srand( ( unsigned int )time( NULL ) );

    for ( int i = 0; i < N; i++ )
    {
        push_front( &head, rand() % N );
    }
    
    display( head );
    
    tri( head );
    
    display( head );

    return 0;
}

答案 1 :(得分:0)

因此 typedef 结构元素 *list; 当你写 列表*我 i 不是元素上的指针而是元素指针上的指针

相关问题