[英]I am getting an error in a C program to sort an array
I am trying to write a C program which sorts an array as follows. 我正在尝试编写一个对数组进行如下排序的C程序。 I am a beginner and I have very less idea about algorithms. 我是一个初学者,对算法的了解很少。 Please help me find the error in my code. 请帮助我在代码中找到错误。 Since I am not getting any compiler errors (except few warnings) but only the program stops responding, so I am not able to detect my error. 由于我没有收到任何编译器错误(除了少数警告),而只有程序停止响应,因此我无法检测到错误。
#include <stdio.h>
#include <stdlib.h>
void selection_sort(int n, int a[n]);
int main () {
int n,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter %d elements :",n);
int a[n];
for (i=0 ; i<n ; i++) {
scanf("%d",&a[i]);
}
selection_sort(n, a[n]);
for (i=0 ; i<n ; i++) {
printf("%d ",a[i]);
}
return(0);
}
void selection_sort(int n, int a[n]) {
int i,p;
p = 0;
if (n != 0) {
for (i=0 ; i<n ; i++) {
if (a[i]>p) p=a[i];
}
p = a[n];
selection_sort(n-1 , a[n]);
}
}
First warnings are not to be ignored. 最初的警告不容忽视。
My compiler says: 我的编译器说:
warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'int *'; 警告:指针转换不兼容的整数,将“ int”传递给类型为“ int *”的参数; take the address with & [-Wint-conversion] 用&[-Wint-conversion]获取地址
selection_sort(n, a[n]);
The is a serious problem: you pass one single integer (and passed end of array...) when you want to pass the array (which will decay to the address of its first element) => this is enough to invoke undefined behaviour and likely to cause a crash 这是一个严重的问题:如果要传递数组(它将衰减到其第一个元素的地址),则传递一个整数(并传递数组的末尾...)=>这足以调用未定义的行为,并且可能导致崩溃
Trivial to fix, just call: 修复起来很简单,只需致电:
selection_sort(n, a);
and later in selection_sort
, recursively call: 然后在selection_sort
,递归调用:
selection_sort(n-1, a);
Once this is done, your algorithm is plain wrong. 一旦完成,您的算法是完全错误的。 For selection sort, you must exchange elements in the array, what you do not. 对于选择排序,必须交换数组中的元素,而不交换数组中的元素。 It should be: 它应该是:
if (n != 0) {
for (i=0 ; i<n ; i++) {
if (a[i]>a[p]) p=i; # first select the rank
}
if (p != n-1) { # then exchange elements
int tmp = a[p];
a[p] = a[n-1];
a[n-1] = tmp;
}
There are few errors in your code. 您的代码中几乎没有错误。
First, when you don't understand everything your are doing, consider warnings as errors. 首先,当您不了解自己所做的一切时,请将警告视为错误。 Because the warnings that are shown during the compilation are errors in the case, and result in segfault. 因为在编译过程中显示的警告是错误的情况,并导致段错误。
When you compile a program, always use flags like gcc -Wall -Werror -Wextra
. 编译程序时,请始终使用gcc -Wall -Werror -Wextra
等标志。 It will treat warnings as errors. 它将警告视为错误。
Second, your program doesn't work because you don't really understand how pointers and arrays work. 其次,您的程序无法运行,因为您并不真正了解指针和数组的工作方式。 When you declare an int array, such as int a[n]
, and try to have access to the array, a[n]
will point to the first element in the array. 当您声明一个int数组(例如int a[n]
)并尝试访问该数组时, a[n]
将指向该数组中的第一个元素。 So if you want to pass the array to a function, you can't pass int a[n]
as a parameter. 因此,如果要将数组传递给函数,则不能将int a[n]
作为参数传递。 Because it is an int. 因为它是一个int。
If you want to pass the array, you have to say "I want to give you the address if that first element, which is &a[0]", which is the same as saying "a". 如果要传递数组,则必须说“如果第一个元素为&a [0],我想给您地址”,这与说“ a”相同。
So what should your function take as a parameter ? 那么,您的函数应将什么作为参数?
If you give "int a[n]" as a parameter, you give it an int. 如果将“ int a [n]”指定为参数,则将其指定为int。 But you want the address of that int, so int *. 但是您想要该int的地址,所以int *。
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.