c - 使用冒泡排序对结构数组进行排序时的警告
问题描述
我创建了 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)
^~~~
解决方案
此错误消息
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
推荐阅读
- java - 如何使用 postgresql 在 jFormattedTextField 中显示日期
- javascript - 如何在不更改选择框值的情况下选择选择框时更改html中的输入框值
- javascript - 如何在单页登录页面中创建滑动效果到可滚动登录页面?
- docker-compose - Docker-compose 如何创建从 Yaml 文件创建 anconda 环境并激活 Enviroment 的服务
- c# - 如何延长 Hangfire 作业的超时时间?
- reactjs - React — 这是对 useCallback 钩子的好用吗?
- sharepoint - 查询有关在 SharePoint 2013 上创建新项目网站的不同方法
- reactjs - 如何在映射所有元素之前显示 ActivityIndicator
- java - 执行查询时 MongoDB Reactive Streams 挂起
- python - 使用掩蔽层进行 LSTM 训练时出错