这是一个程序,用于打印最小值和它在数组中的位置(由用户定义)。
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[n];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<=n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
但是在运行它时,它显示以下错误:
Segmentation fault (core dumped)
可能是什么原因?
答案 0 :(得分:2)
其中一条注释中所述,第一个错误是在甚至不知道n是多少之前,声明了一个大小为n的数组。
第二个错误是主函数中的for循环,该循环从0到n,即数组的索引超出范围。
尝试一下:
int main() {
int n = 0, j = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for (j = 0; j < n; j++) {
printf("a[%d] = ", j + 1);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
如果这解决了您的问题,请对其进行标记。
答案 1 :(得分:1)
首先:
int n, j;
都未初始化。初始化它们,否则您将获得垃圾值。
int n = 0, j = 0;
如果n
(很可能)在下一行0
是怎么回事?
int a[n];
您为数组0
分配了a[]
个字节。然后,在下面的行中输入10
scanf("%d", &n);
您将在下面的segmentation fault
循环中获得for()
,因为您的循环正试图将10
元素放在根本没有分配内存的位置。
如果偶然(很可能)未初始化的n
为2 ^ 32 * 4个字节(最大int
s)会发生什么?
int a[n];
您为数组a[]
分配了2 ^ 32字节。然后,在下面的行中输入10
scanf("%d", &n);
您将不会获得segmentation fault
,但将为程序分配2 ^ 32 * 4字节的内存,并且仅使用10
第二个还不够:
for (j = 0; j <= n; ++j)
scanf("%d", arr[n];
将访问数组11th
的元素,这是未定义的行为,您甚至可以在其中得到segmentation fault
。如您所知,C
数组中的数组从0
到n - 1
进行索引(如果n
是数组的大小)。
第三次在函数内部循环
for(i=0; i<=n-1; i=i+1)
与以下相同:
for(i=0; i < n; ++i)
最后,我相信您的代码中有一个错误:
if(a[i]<a[0])
应为:
if (a[i] < smallest)
因为您很可能希望将其他数字与已经最小的元素进行比较而不是与a[0]
进行比较。这是我的代码
#include <stdio.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i = 0, k=0;
for(i=0; i<n; ++i) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for(j=0; j<n; ++j) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
以上版本是C99
及更高版本的合法版本。如果您使用的是C89
和更早的编译器,则堆栈的大小固定,如@Saurabh的答案所述,或者最好使用malloc()
。
这是malloc()
版本:
#include <stdio.h>
#include <stdlib.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i=0,k=0;
for(i=0; i<=n-1; i=i+1) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *a = malloc(sizeof(int) * n);
for(j=0; j<n; j=j+1) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
答案 2 :(得分:1)
使用这个:
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[100];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
除了根据用户的输入运行循环外,还可以指定数组的大小。
函数position_smallest
中存在一些问题。因此,如果您要纠正它,请查看@Gox的答案。