c - 当我使用指针跟踪数组中的当前位置时,为什么我不断收到 Segfault 11?

标签 c

我正在编写一个程序来查找 int 数组的最大值和混合值,并且我试图避免使用 int 来跟踪当前位置,但程序不断抛出段错误 11。它不会去通过循环。什么原因?

/* Finds the largest and smallest elements in an array */
#include <stdio.h>
#define N 10

void max_min(int a[], int n, int *max, int *min);

int main(void){
int b[N], i, *big, *small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
   scanf("%d", &b[i]);
max_min(b, N, big, small);
printf("Largest: %d\n", *big);
printf("Smallest: %d\n", *small);
return 0;
}

void max_min(int a[], int n, int *max, int *min){
    int *p;
    *max = *min = *p = a[0]; 
    while (*p != EOF) {
        if (*p > *max)
           max = p;
        else if (*p < *min)
           min = p;
        p++;
    } 
}

最佳答案

好的,有一些错误。我制作了两个版本。第一个注释了错误,第二个注释了已清理的内容。

这是带注释的版本[请原谅无偿的风格清理]:

/* Finds the largest and smallest elements in an array */
#include <stdio.h>
#define N 10

void max_min(int a[], int n, int *max, int *min);

int
main(void)
{
    int b[N],
     i,
    *big,
    *small;

    printf("Enter %d numbers: ", N);
    for (i = 0; i < N; i++)
        scanf("%d", &b[i]);

    // BUG1: big/small have not been initialized to point to anything
    max_min(b, N, big, small);

    printf("Largest: %d\n", *big);
    printf("Smallest: %d\n", *small);

    return 0;
}

void
max_min(int a[], int n, int *max, int *min)
{
    int *p;

    // BUG2: because of BUG1 above, this will segfault
    // BUG3: because p is never initialized, dereferencing it (via "*p") will
    // segfault
    *max = *min = *p = a[0];

    // BUG4: this will run past the end of the "a" array because there is
    // no guarantee of a matching sentinel value (i.e. EOF/-1)
    // in fact, using a sentinel is wrong because the "a" array can have _any_
    // value (i.e. there is _no_ sentinel value that can be used)
    while (*p != EOF) {
        // BUG5: when max or min gets set, we're changing what they point to
        // but this will _not_ change caller's values
        if (*p > *max)
            max = p;
        else if (*p < *min)
            min = p;
        p++;
    }
}

这是一个清理后的工作版本。特别要注意 main 中的 big/smallint *int 的更改以及对 max_min 的调用中的相应更改:

/* Finds the largest and smallest elements in an array */
#include <stdio.h>
#define N 10

void max_min(int a[], int n, int *max, int *min);

int
main(void)
{
    int b[N],
     i,
    big,
    small;

    printf("Enter %d numbers: ", N);
    for (i = 0; i < N; i++)
        scanf("%d", &b[i]);

    max_min(b, N, &big, &small);

    printf("Largest: %d\n", big);
    printf("Smallest: %d\n", small);

    return 0;
}

void
max_min(int a[], int n, int *max, int *min)
{
    int *p;
    int val;

    p = &a[0];
    *max = *min = *p;

    for (int i = 0;  i < n;  ++i, ++p) {
        val = *p;
        if (val > *max)
            *max = val;
        else if (val < *min)
            *min = val;
    }
}

关于c - 当我使用指针跟踪数组中的当前位置时,为什么我不断收到 Segfault 11?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38087590/

相关文章:

c - 打印内容链表

c - 绕圆运动的点的三角测量算法

在 printf 之后调用 Opendir 时 C 程序崩溃

c:释放 char* 而不破坏 char**

c - Java 和 C 代码之间的相似性

使用sprintf在C中连接整数和字符

c++ - 在 Ncurses 上添加一个滚动条或者让它像 "more"

c - Printf w/Eclipse CDT 问题

控制网络带宽

c - 多重测试 scanf 结果 - C