首页 > 解决方案 > 使用冒泡排序对结构数组进行排序时的警告

问题描述

我创建了 2 个结构,一种称为产品,一种称为订单。

我想要的是对一个由订单组成的数组进行排序,这些订单包括一个产品数组,我使用冒泡排序算法来做到这一点。

该程序运行良好,但问题是它给出了一些我似乎无法理解的奇怪警告。

结构:

产品:

/* Structures */
typedef struct product 
{
   int ident;
   char desc[MAX_CHARS]; /* string that describes a product eg. "bread" */
   int price;  /* price of the product*/
   int weight; /* weight of the product eg. 2kg */
   int quant; /* quantity of the product in stock */
   int state_prod;
}product;

命令:


typedef struct order 
{
   int ident_o;
   product set_prod[MAX_PRODS_OD]; /* Set of products */
   int state;
}order;

冒泡排序算法:

void swap(char * xp,char * yp) 
{ 
    char * temp = xp; 
    xp = yp; 
    yp = temp; 
} 

/* A function to implement bubble sort */
void bubbleSort(product arr[], int n) 
{ 
   int i, j; 
   for (i = 0; i < n-1; i++)       

       /* Last i elements are already in place */ 
       for (j = 0; j < n-i-1; j++)  
           if (arr[j].desc > arr[j+1].desc) 
              swap(&arr[j].desc, &arr[j+1].desc); 
} 

我只能使用此命令“gcc -g -Wall -Wextra -Werror -ansi -pedantic”进行编译。

警告:

In function ‘bubbleSort’:
error: passing argument 1 of ‘swap’ from incompatible pointer type [-Werror=incompatible-pointer-types]
               swap(&arr[j].desc, &arr[j+1].desc);
                    ^
note: expected ‘char *’ but argument is of type ‘char (*)[64]’
 void swap(char * xp,char * yp)
      ^~~~

error: passing argument 2 of ‘swap’ from incompatible pointer type [-Werror=incompatible-pointer-types]
               swap(&arr[j].desc, &arr[j+1].desc);
                                  ^
note: expected ‘char *’ but argument is of type ‘char (*)[64]’
 void swap(char * xp,char * yp)
      ^~~~

标签: carrayssortingcompiler-warningsbubble-sort

解决方案


此错误消息

error: passing argument 1 of ‘swap’ from incompatible pointer type [-Werror=incompatible-pointer-types]
               swap(&arr[j].desc, &arr[j+1].desc);

意味着您向函数传递了一个指向声明为的数组的指针

char desc[MAX_CHARS];

使用例如&arr[j].desc具有类型的表达式char( * )[MAX_CHARS]。但是函数参数具有类型char *。你至少要写

swap( arr[j].desc, arr[j+1].desc);

但是函数 swap 本身是不正确的。它不交换字符数组的元素。

void swap(char * xp,char * yp) 
{ 
    char * temp = xp; 
    xp = yp; 
    yp = temp; 
} 

例如,您可以通过以下方式定义它

void swap( char *xp, char *yp ) 
{ 
    char temp[MAX_CHARS];
    strcpy( temp, xp );
    strcpy( xp, yp );
    strcpy( yp, temp ); 
} 

这是一个演示程序

#include <stdio.h>
#include <string.h>

#define MAX_CHARS   10

typedef struct product 
{
   char desc[MAX_CHARS]; /* string that describes a product eg. "bread" */
} product;

void swap( char *s1, char *s2 )
{
    char temp[MAX_CHARS];
    strcpy( temp, s1 );
    strcpy( s1, s2 );
    strcpy( s2, temp );
}

int main(void) 
{
    product p1 = { "Hello" };
    product p2 = { "World" };

    printf( "p1.desc = %s\n", p1.desc );
    printf( "p2.desc = %s\n", p2.desc );

    putchar( '\n' );

    swap( p1.desc, p2.desc );

    printf( "p1.desc = %s\n", p1.desc );
    printf( "p2.desc = %s\n", p2.desc );

    return 0;
}

它的输出是

p1.desc = Hello
p2.desc = World

p1.desc = World
p2.desc = Hello

在 if 语句中

if (arr[j].desc > arr[j+1].desc) 

你必须比较字符串本身

if ( strcmp( arr[j].desc, arr[j+1].desc ) > 0 )

还要注意你没有交换结构。您只是交换他们的数据成员 desc,这与对结构进行排序不同。

要交换结构类型的对象,函数 swap 可以如下所示

void swap( product *p1, product *p2 )
{
    product temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

这是一个演示程序。

#include <stdio.h>

#define MAX_CHARS   10

typedef struct product 
{
   char desc[MAX_CHARS]; /* string that describes a product eg. "bread" */
} product;

void swap( product *p1, product *p2 )
{
    product temp = *p1;
    *p1 = *p2;
    *p2 = temp; 
}

int main(void) 
{
    product p1 = { "Hello" };
    product p2 = { "World" };

    printf( "p1.desc = %s\n", p1.desc );
    printf( "p2.desc = %s\n", p2.desc );

    putchar( '\n' );

    swap( &p1, &p2 );

    printf( "p1.desc = %s\n", p1.desc );
    printf( "p2.desc = %s\n", p2.desc );

    return 0;
}

它的输出是

p1.desc = Hello
p2.desc = World

p1.desc = World
p2.desc = Hello

推荐阅读