将 **array + a 更改为 *array[a]

标签 c arrays pointers

我的程序所做的是找到数组中最接近平均值的 2 个数字,一个较大,一个较小。它工作正常,但是我需要将 **array+a 更改为 *array[a]

但是,当我加载程序时,输入数字后程序崩溃了。如果我尝试打印 *array[0]*array[1] 等,它工作正常。当我尝试打印或仅使用 *array[a]*array[b] 执行某些操作时,它会崩溃。感谢您的帮助。

#include <stdio.h>
#include <stdlib.h>
int  input    (int *t, int *array[]);
void calculation (int *array[], int *t, int *x, int *y);
void output   (int *x, int *y);

int main()
{
    int *array, t, x, y;
    input (&t, &array);
    calculation (&array, &t, &x, &y);
    output (&x, &y);
    return 0;
}
int input (int *t, int *array[])
{   int n, *ptr;
    printf ("How big is the array?");
    scanf ("%d", &n);
    ptr = (int*) malloc(n * sizeof(int));
    int k;
    printf ("Enter the numbers:");
    for (k=0; k<n; k++)
    {   scanf ("%d", ptr + k);
    }
    *t=n;
    *array=ptr;
    return 0;
}
void calculation (int *array[], int *t, int *x, int *y)
{   float sum=0, avg;
    int min, max; 
    int more, less; 
    int a, b, c;  
    for (a=0; a<(*t); a++)
        {sum=sum+ **array + a;
        }
    avg=sum/(*t);
    min= *array[0];
    max= *array[0];

    for (b=0; b<(*t); b++)
    {       if (max < (**array + b)) max=(**array + b);
            if (min > (**array + b)) min=(**array + b);
    }
    more=max;
    less=min;
    for (c=0; c<(*t); c++)
    {   if (((**array + c) < avg) && ((**array + c) > less)) less=(**array + c);
        if (((**array + c) > avg) && ((**array + c) < more)) more=(**array + c);
    }
    *x=less;
    *y=more;
}

void output (int *x, int *y)
{       printf("Number that is less than the average:%d\n", *x);
        printf("Number that is more than the average:%d\n", *y);
}

最佳答案

最好重新考虑一下你的函数原型(prototype)。将指向 array 的指针传递给 input() 函数是有意义的,因为您正在为其分配内存,并且您希望在返回时能够访问它。但你不需要传入int t的指针;相反,只需返回 n 的值,并将其分配给 main 中的 t 即可。

没有理由将指向array的指针传递给函数calculation(),因为您没有更改数组分配。您还可以从 main() 传入 t 的值,因为您只在 calculation() 中使用该值,但不要更改它。

同样,output() 函数只需要 xy 的副本,因为它不会更改它们。

这里的经验法则是,当您想要修改函数内的值并有权访问调用函数中修改后的值时,请将指向值的指针传递到函数中。但您也可以返回一个值,而不是使用指向它的指针。

这些更改不会改变代码的功能,但会大大提高其可读性。您甚至可以通过查看函数原型(prototype)来了解每个函数中正在修改的内容。好吧,这些更改确实改变了功能,因为您原来的 **array + a 不正确,需要是 *(*array + a) (*数组)[a]。但是解决这个问题应该可以帮助您欣赏更简单的函数原型(prototype)的优点。下面是修改后的代码:

#include <stdio.h>
#include <stdlib.h>

int input(int *array[]);
void calculation(int array[], int t, int *x, int *y);
void output(int x, int y);

int main(void)
{
    int *array, t, x, y;

    t = input(&array);
    calculation(array, t, &x, &y);
    output(x, y);
    return 0;
}
int input(int *array[])
{   int n, *ptr;
    printf("How big is the array?");
    scanf("%d", &n);
    ptr = (int*) malloc(n * sizeof(int));
    int k;
    printf("Enter the numbers:");
    for (k=0; k<n; k++)
    {   scanf("%d", ptr + k);
    }
    *array=ptr;
    return n;
}
void calculation(int array[], int t, int *x, int *y)
{   float sum=0, avg;
    int min, max; 
    int more, less; 
    int a, b, c;  
    for (a=0; a<t; a++)
    {sum=sum+ array[a];
    }
    avg=sum/t;
    min= array[0];
    max= array[0];

    for (b=0; b<t; b++)
    {       if (max < array[b]) max=array[b];
        if (min > array[b]) min=array[b];
    }
    more=max;
    less=min;
    for (c=0; c<t; c++)
    {   if ((array[c] < avg) && (array[c] > less)) less=array[c];
        if ((array[c] > avg) && (array[c] < more)) more=array[c];
    }
    *x=less;
    *y=more;
}

void output(int x, int y)
{       printf("Number that is less than the average:%d\n", x);
    printf("Number that is more than the average:%d\n", y);
}

关于将 **array + a 更改为 *array[a],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179898/

相关文章:

c - 从结构中的指针访问结构的值

c++ - "this"的值什么时候偏移了一个偏移量?

c - C 语言远程桌面项目

c++ - 取消引用指针以制作数组的拷贝

c - 将指针传递给函数异常

javascript - 如何在每次循环迭代中一个一个地移动数组的 3 个元素?

C++ - 字符数组以某种方式初始化为错误的大小

c - 程序执行输出

c - 在 OpenMP 中使用任务指令的正确方法是什么

c - 我怎样才能在c中的两个printf之间等待一秒钟?