c - 将数组传递给已扫描的函数

标签 c arrays function

我正在努力将之前扫描过的数组数据发送到一个函数。

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

int display(int a[21]);

int main(){
    int i;
    int a[21];
    FILE*fpointer;
    fpointer=fopen("data.txt","r");
    if (fpointer==NULL){
        printf("\nFile could not be found.");
        exit(1);
    }
    else{
        for (i=0;i<21;i++){
            fscanf(fpointer, "%d",&a[i]);   
        }
        fclose(fpointer);
    }
    display(int a[21]);
    return 0;
}

int display(int a[21]){
    int i;
    for (i=0;i<21;i++){
        printf("%d\n",a[i]);
    }
    return 0;
}

我想使用 display() 函数显示 data.txt 中的每个值。我能够 fscanf every 进入数组,我似乎无法将数组发送到函数。

数据.txt:

1
6
4
7
3
4
12
14
15
-17
-19
21
-23
0
37
0
-31
32
34
-37
-39

输出值:

1065353216
1086324736
1082130432
1088421888
1077936128
1082130432
1094713344
1096810496
1097859072
-1048051712
-1047003136
1101529088
-1044905984
0
1108606976
0
-1040711680
1107296256
1107820544
-1038876672
-1038352384

最佳答案

main 的末尾,这不是函数调用:

        …
    }
    display(int a[21]);
    return 0;
}

因为包含了参数的类型,所以这被认为是一个声明(没有返回类型,所以隐含地是int)。要调用函数,只需传递所需的参数:

display(a);

关于c - 将数组传递给已扫描的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47312529/

相关文章:

c - 在遇到一定数量的换行符后,我的程序会返回我想要的正确位置吗?

php - 如何将没有键的数组插入mysql?

javascript - 如何创建一个函数来接受参数并在调用时递增它?

c - 如何使用 USB 端口将数据从 Arduino Uno 发送到 PC

c - 如何理解/可视化指向数组第一个索引的概念数组变量名?

c - C 中 fwrite 和 struct 的奇怪错误

javascript - 有人可以解释一下函数中的一行代码吗

javascript - 转换 JavaScript 二维数组

javascript - 将数组转换为对象

algorithm - 当你只有一大组输入/输出对时,你如何找到函数的定义?