c语言中qsort函数的使用、编程中的一些错误

qsort()函数:

功能:相当于c++sort,具有快排的功能,复杂度的话nlog(n)
注:C中的qsort()采用的是快排算法,C++的sort()则是改进的快排算法。两者的时间复杂度都是nlogn,但是实际应用中,sort()一般要快些,建议使用sort()。
头文件:stdlib.h
用法:  void qsort(void* base,size_t_num,size_t_width,int(__cdecl*compare)(const void*,const void*));
  参数:1、待排序数组,排序之后的结果仍放在这个数组中
     2、数组中待排序元素数量
     3、各元素的占用空间大小(单位为字节)
          4、指向函数的指针,用于确定排序的顺序(需要用户自定义一个比较函数)

1、对int类型进行排序:

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int num[100];
 4 int cmp_int(const void* aa,const void* bb)
 5 {
 6     int* a=(int*)aa;
 7     int* b=(int*)bb;
 8     return *a-*b;
 9 }
10 int main()
11 {
12     int i;
13     num[1]=3;
14     num[2]=2;
15     num[3]=1;
16     qsort(num+1,3,sizeof(int),cmp_int);
17     for(i=1;i<=3;++i)
18         printf("%d
",num[i]);
19 }

2、对double类型进行排序

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 double num[100];
 4 int cmp_double(const void* aa,const void* bb)
 5 {
 6     double* a=(double*)aa;
 7     double* b=(double*)bb;
 8     return *a-*b;
 9 }
10 int main()
11 {
12     int i;
13     num[1]=3.5;
14     num[2]=3.1;
15     num[3]=3.3;
16     qsort(num+1,3,sizeof(double),cmp_double);
17     for(i=1;i<=3;++i)
18         printf("%lf
",num[i]);
19 }

3、对结构体进行排序:

 1 #include<stdlib.h>
 2 #include<stdio.h>
 3 typedef struct NODE
 4 {
 5     int number;
 6     int grade;
 7 } NODE;  //这样的话,后面才能找到这个新声明的类型
 8 int Comp(const void *p1, const void *p2)
 9 {
10     NODE *c = (NODE*)p1;
11     NODE *d = (NODE*)p2;
12     if(c->grade != d->grade)
13         return c->grade - d->grade;
14     else
15         return c->number - d->number;
16 }
17 NODE node[101];//数组一般需要大小固定
18 int main()
19 {
20     int n;
21     while(scanf("%d", &n) != EOF)
22     {
23         int i;
24         for( i = 0; i < n; i++)
25             scanf("%d%d",&node[i].number,&node[i].grade);
26         qsort(node, n, sizeof(NODE), Comp);
27         for(i = 0; i<n; i++)
28             printf("%d %d
",node[i].number,node[i].grade);
29     }
30     return 0;
31 }

补充两个:

1、expected expression before错误表示你这一行有一些函数名字没有写全

2、stray '241' in program|

这个其实很简单,就是你错误所在行的代码或者中文出现了非法字符。

解决办法:

1.把出错行的空格及其前后空格删掉重新打一下试试。

  1 1、意思是:在 xxx 之前 应输入表达式。
  2 2、下面为C语言的错误大全及中文解释:
  3 1: Ambiguous operators need parentheses — 不明确的运算需要用括号括起
  4 2: Ambiguous symbol xxx — 不明确的符号
  5 3: Argument list syntax error — 参数表语法错误
  6 4: Array bounds missing — 丢失数组界限符
  7 5: Array size toolarge — 数组尺寸太大
  8 6: Bad character in paramenters — 参数中有不适当的字符
  9 7: Bad file name format in include directive — 包含命令中文件名格式不正确
 10 8: Bad ifdef directive synatax — 编译预处理ifdef有语法错
 11 9: Bad undef directive syntax — 编译预处理undef有语法错
 12 10: Bit field too large — 位字段太长
 13 11: Call of non-function — 调用未定义的函数
 14 12: Call to function with no prototype — 调用函数时没有函数的说明
 15 13: Cannot modify a const object — 不允许修改常量对象
 16 14: Case outside of switch — 漏掉了case 语句
 17 15: Case syntax error — Case 语法错误
 18 16: Code has no effect — 代码不可能执行到
 19 17: Compound statement missing{ — 分程序漏掉"{"
 20 18: Conflicting type modifiers — 不明确的类型说明符
 21 19: Constant expression required — 要求常量表达式
 22 20: Constant out of range in comparison — 在比较中常量超出范围
 23 21: Conversion may lose significant digits — 转换时会丢失意义的数字
 24 22: Conversion of near pointer not allowed — 不允许转换近指针
 25 23: Could not find file xxx — 找不到XXX文件
 26 24: Declaration missing ; — 说明缺少""
 27 25: Declaration syntax error — 说明中出现语法错误
 28 26: Default outside of switch — Default 出现在switch语句之外
 29 27: Define directive needs an identifier — 定义编译预处理需要标识符
 30 28: Division by zero — 用零作除数
 31 29: Do statement must have while — Do-while语句中缺少while部分
 32 30: Enum syntax error — 枚举类型语法错误
 33 31: Enumeration constant syntax error — 枚举常数语法错误
 34 32: Error directive :xxx — 错误的编译预处理命令
 35 33: Error writing output file — 写输出文件错误
 36 34: Expression syntax error — 表达式语法错误
 37 35: Extra parameter in call — 调用时出现多余错误
 38 36: File name too long — 文件名太长
 39 37: Function call missing ) — 函数调用缺少右括号
 40 38: Fuction definition out of place — 函数定义位置错误
 41 39: Fuction should return a value — 函数必需返回一个值
 42 40: Goto statement missing label — Goto语句没有标号
 43 41: Hexadecimal or octal constant too large — 16进制或8进制常数太大
 44 42: Illegal character x — 非法字符x
 45 43: Illegal initialization — 非法的初始化
 46 44: Illegal octal digit — 非法的8进制数字 A
 47 45: Illegal pointer subtraction — 非法的指针相减
 48 46: Illegal structure operation — 非法的结构体操作
 49 47: Illegal use of floating point — 非法的浮点运算
 50 48: Illegal use of pointer — 指针使用非法
 51 49: Improper use of a typedefsymbol — 类型定义符号使用不恰当
 52 50: In-line assembly not allowed — 不允许使用行间汇编
 53 51: Incompatible storage class — 存储类别不相容
 54 52: Incompatible type conversion — 不相容的类型转换
 55 53: Incorrect number format — 错误的数据格式
 56 54: Incorrect use of default — Default使用不当
 57 55: Invalid indirection — 无效的间接运算
 58 56: Invalid pointer addition — 指针相加无效
 59 57: Irreducible expression tree — 无法执行的表达式运算
 60 58: Lvalue required — 需要逻辑值0或非0值
 61 59: Macro argument syntax error — 宏参数语法错误
 62 60: Macro expansion too long — 宏的扩展以后太长
 63 61: Mismatched number of parameters in definition — 定义中参数个数不匹配
 64 62: Misplaced break — 此处不应出现break语句
 65 63: Misplaced continue — 此处不应出现continue语句
 66 64: Misplaced decimal point — 此处不应出现小数点
 67 65: Misplaced elif directive — 不应编译预处理elif
 68 66: Misplaced else — 此处不应出现else
 69 67: Misplaced else directive — 此处不应出现编译预处理else
 70 68: Misplaced endif directive — 此处不应出现编译预处理endif
 71 69: Must be addressable — 必须是可以编址的
 72 70: Must take address of memory location — 必须存储定位的地址
 73 71: No declaration for function xxx — 没有函数xxx的说明
 74 72: No stack — 缺少堆栈
 75 73: No type information — 没有类型信息
 76 74: Non-portable pointer assignment — 不可移动的指针(地址常数)赋值
 77 75: Non-portable pointer comparison — 不可移动的指针(地址常数)比较
 78 76: Non-portable pointer conversion — 不可移动的指针(地址常数)转换
 79 77: Not a valid expression format type — 不合法的表达式格式
 80 78: Not an allowed type — 不允许使用的类型
 81 79: Numeric constant too large — 数值常太大
 82 80: Out of memory — 内存不够用
 83 81: Parameter xxx is never used — 能数xxx没有用到
 84 82: Pointer required on left side of -> — 符号->的左边必须是指针
 85 83: Possible use of xxx before definition — 在定义之前就使用了xxx(警告)
 86 84: Possibly incorrect assignment — 赋值可能不正确
 87 85: Redeclaration of xxx — 重复定义了xxx
 88 86: Redefinition of xxx is not identical — xxx的两次定义不一致
 89 87: Register allocation failure — 寄存器定址失败
 90 88: Repeat count needs an lvalue — 重复计数需要逻辑值
 91 89: Size of structure or array not known — 结构体或数给大小不确定
 92 90: Statement missing ; — 语句后缺少""
 93 91: Structure or union syntax error — 结构体或联合体语法错误
 94 92: Structure size too large — 结构体尺寸太大
 95 93: Sub scripting missing ] — 下标缺少右方括号
 96 94: Superfluous & with function or array — 函数或数组中有多余的"&"
 97 95: Suspicious pointer conversion — 可疑的指针转换
 98 96: Symbol limit exceeded — 符号超限
 99 97: Too few parameters in call — 函数调用时的实参少于函数的参数不
100 98: Too many default cases — Default太多(switch语句中一个)
101 99: Too many error or warning messages — 错误或警告信息太多
102 100: Too many type in declaration — 说明中类型太多
103 101: Too much auto memory in function — 函数用到的局部存储太多
104 102: Too much global data defined in file — 文件中全局数据太多
105 103: Two consecutive dots — 两个连续的句点
106 104: Type mismatch in parameter xxx — 参数xxx类型不匹配
107 105: Type mismatch in redeclaration of xxx — xxx重定义的类型不匹配
108 106: Unable to create output file xxx — 无法建立输出文件xxx
109 107: Unable to open include file xxx — 无法打开被包含的文件xxx
110 108: Unable to open input file xxx — 无法打开输入文件xxx
111 109: Undefined label xxx — 没有定义的标号xxx
112 110: Undefined structure xxx — 没有定义的结构xxx
113 111: Undefined symbol xxx — 没有定义的符号xxx
114 112: Unexpected end of file in comment started on line xxx — 从xxx行开始的注解尚未结束文件不能结束
115 113: Unexpected end of file in conditional started on line xxx — 从xxx 开始的条件语句尚未结束文件不能结束
116 114: Unknown assemble instruction — 未知的汇编结构
117 115: Unknown option — 未知的操作
118 116: Unknown preprocessor directive: xxx — 不认识的预处理命令xxx
119 117: Unreachable code — 无路可达的代码
120 118: Unterminated string or character constant — 字符串缺少引号
121 119: User break — 用户强行中断了程序
122 120: Void functions may not return a value — Void类型的函数不应有返回值
123 121: Wrong number of arguments — 调用函数的参数数目错
124 122: xxx not an argument — xxx不是参数
125 123: xxx not part of structure — xxx不是结构体的一部分
126 124: xxx statement missing ( — xxx语句缺少左括号
127 125: xxx statement missing ) — xxx语句缺少右括号
128 126: xxx statement missing ; — xxx缺少分号
129 127: xxx declared but never used — 说明了xxx但没有使用
130 128: xxx is assigned a value which is never used — 给xxx赋了值但未用过 
原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/11856888.html