我该如何传递大小由用户输入确定的数组,并在函数中传递该数组?

(C编程)我想知道如何将一个数组传递给一个大小直到编译时才知道的函数?总的来说,我的程序从用户那里接收数字(双精度数),并使用scanf()将所有数字存储到数组中,但0除外。只要输入0,就应该将其丢弃(不以任何方式存储),并且用户应该能够继续。来自用户的输入数量是未知的,但是最多不会有1百万(10 ^ 6)个输入数量,或者直到用户输入EOF(ctrl d)为止。 (不要求用户输入数组的大小,仅输入值)。然后,我必须将数组传递给一个单独的函数,该函数将确定数组的总和。我有一个函数count()对数组中的数字进行计数,这似乎可以正常工作,但是我的函数sums()似乎没有访问数组的元素,因为它每次都返回0。我知道我可能需要一个指针,但是不确定如何进行操作。到目前为止,我的程序在下面。

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

#define MAX_SIZE 10*10*10*10*10*10

int main();
int counts(double array[]);
double sums(double array[]);
double maxFind(double array[]);


int main()
{
double arr[MAX_SIZE];
int countNums,i;
double sum,max;

printf("Please enter values\n");

while(scanf("%f",&arr[i]) != EOF && i < MAX_SIZE)
{
    if(arr[i] == 0){
        continue;
    }
    i++;
}




countNums = counts(arr);
printf("The count of numbers read from the input is %9d\n",countNums);

sum = sums(arr);
printf("The sum of these numbers is %20.10f\n",sum);

max = maxFind(arr);
printf("The max number is %20.10f\n",max);
}




int counts(double array[])
{
int countNum = 0;
    for(int i = 0; i < sizeof(array) ; i++)
{
    if(array[i] == 0){
        continue;
    }
       countNum++;
}
return countNum;
}



double sums(double array[])
{
int i;
double sumNum = 0;
for(i = 0; i < sizeof(array); i++){
    sumNum = sumNum + array[i];
}
return sumNum;
}



double maxFind(double array[])
{
double maxNum = array[0];
for(int i = 1; i < sizeof(array); i++){
    if(array[i] > array[i-1]){
        maxNum = array[i];
    }
}
return maxNum;
}
fancykee 回答:我该如何传递大小由用户输入确定的数组,并在函数中传递该数组?

您面对的经典案例是“如何读取未知数量的X?”。答案是简单地使用malloc(或callocrealloc)分配一些初始数量的元素,并跟踪您拥有的allocated,然后跟踪元素数量{{ 1}}(已填充)。当used时,您used == allocated数组并将可用元素的数量增加一定数量,或者每次增加一些固定数量,或者增加一些当前数量。元素数量加倍是分配增长和所需重新分配数量的相当不错的平衡。

一个简单的示例,用realloc读取未知数目的双精度数,然后输出读取的数字和总和可能是:

scanf

注意:您总是#include <stdio.h> #include <stdlib.h> int main (void) { size_t used = 0,allocated = 2; /* used doubles/allocated doubles */ double *arr,sum = 0.0; /* array & sum of elements */ /* allocate initial 'allocated' number of doubles */ if (!(arr = malloc (allocated * sizeof *arr))) { perror ("malloc-arr"); return 1; } while (scanf ("%lf",&arr[used]) == 1) { /* read doubles until EOF */ sum += arr[used++]; /* add to sum,increment used */ if (used == allocated) { /* if used == allocated */ /* reallocate more doubles using a temporary pointer */ void *tmp = realloc (arr,2 * allocated * sizeof *arr); if (!tmp) { /* validate reallocation */ perror ("realloc-tmp"); break; /* don't exit,original arr still good,break */ } arr = tmp; /* assign reallocated block to arr */ allocated *= 2; /* update number allocated */ } } printf ("%zu doubles entered with sum: %.2f\n",used,sum); free (arr); /* free the memory you have allocated */ } 到临时指针,因为如果realloc失败,它将返回realloc覆盖原始指针地址,从而使您无法能够访问或NULL realloc`确实失败,通过使用临时指针,您的现有数据将通过原始指针保留)

您现在可以将free() the original block resulting in a *memory leak*. If及其包含的元素数(arr)传递给需要该信息的任何函数。

使用/输出示例

used

或从文件中重定向10,000个浮点值:

$ ./bin/sumdoubles
1.1
2.2
3.3
4.4
5.5
6.6
7.7
8.8
9.9
9 doubles entered with sum: 49.50

内存使用/错误检查

在您编写的任何动态分配内存的代码中,对于任何分配的内存块,您都有2个职责:(1)始终保留指向起始地址的指针因此,(2)当不再需要它时可以释放

当务之急是使用一个内存错误检查程序来确保您不尝试访问内存或不在分配的块的边界之外/之外写,尝试读取或基于未初始化的值进行条件跳转,最后,以确认您释放了已分配的所有内存。

对于Linux,$ ./bin/sumdoubles < ../dat/10000float.txt 10000 doubles entered with sum: 117991733.64 是正常选择。每个平台都有类似的内存检查器。它们都很容易使用,只需通过它运行程序即可。

valgrind

始终确认已释放已分配的所有内存,并且没有内存错误。

编辑-功能中的其他问题

$ valgrind ./bin/sumdoubles
==6316== Memcheck,a memory error detector
==6316== Copyright (C) 2002-2017,and GNU GPL'd,by Julian Seward et al.
==6316== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6316== Command: ./bin/sumdoubles
==6316==
1.1
2.2
3.3
4.4
5.5
6.6
7.7
8.8
9.9
9 doubles entered with sum: 49.50
==6316==
==6316== HEAP SUMMARY:
==6316==     in use at exit: 0 bytes in 0 blocks
==6316==   total heap usage: 6 allocs,6 frees,2,288 bytes allocated
==6316==
==6316== All heap blocks were freed -- no leaks are possible
==6316==
==6316== For counts of detected and suppressed errors,rerun with: -v
==6316== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

for(i = 0; i < sizeof(array); i++) 就是sizeof(array)。数组在访问时转换为指针。请参阅:C11 Standard - 6.3.2.1 Other Operands - Lvalues,arrays,and function designators(p3)。您可能希望同时传递指针和元素数量,例如

sizeof(a_pointer)

您将在double sums(double *array,size_t n) { size_t i; double sumNum = 0; for(i = 0; i < n; i++){ sumNum = sumNum + array[i]; } return sumNum; } 中以{p>的身分呼叫sums

main()

使用size_t sum = sums (arr,used); 函数的示例(经过稍微修改以使其具有品味):

sums

(输出将相同)

仔细检查一下,如果还有其他问题,请告诉我。

本文链接:https://www.f2er.com/3162531.html

大家都在问