1.对二维int数组排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int cmp(const void*a,const void*b){
int *c=(int *)a;
int *d=(int *)b;
if(*c!=*d) return *c-*d; //此为升序排列
else return *(c+1)-*(d+1); //降序改为 *(d+1)-*(c+1);
}
int main(){
srand((int)time(0));
int num[10][2];
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int cmp(const void*a,const void*b){
int *c=(int *)a;
int *d=(int *)b;
if(*c!=*d) return *c-*d; //此为升序排列
else return *(c+1)-*(d+1); //降序改为 *(d+1)-*(c+1);
}
int main(){
srand((int)time(0));
int num[10][2];

这篇博客介绍了如何使用qsort函数对二维字符型数组进行排序。示例代码展示了一个包含6个字符串的二维数组,通过自定义比较函数cmp实现了数组元素的升序排列,并在排序后打印了结果。
最低0.47元/天 解锁文章
1100

被折叠的 条评论
为什么被折叠?



