c - 将数组传递给函数

标签 c arrays function

我正在尝试将用户从 scanf( "%d", &ar[rows][cols] ); 输入的值获取到 int 变量 temp.

但不知怎的,当我执行时,它在printf(“请输入9个正整数:”);之后给出了一个错误

编辑:我忘了包含代码。代码如下:

/* File: StudentID_Surname.c  - e.g. 1234567_Wilson.c
 * This program finds the range between highest and lowest value of a 2-D array */

#include <stdio.h>

#define NROW 3
#define NCOL 3

/* Write a function
     void disp_arr(int a[NROW][NCOL]) { ... }
    where a[][] is the 2-D array
    Print the entire array to the screen. */

disp_arr( temp );

int main(void)
{
    /* declare needed variables or constants, e.g. */
    int ar[NROW][NCOL];
    int rows, cols, temp;

    /* prompt for the user to enter nine positive integers to be stored into the array */

    for ( rows = 0 ; rows < 3 ; rows++ )
    {
        for ( cols = 0 ; cols < 3 ; cols++ )
            {
                printf(  "Please enter 9 positive integers : " );

                scanf( "%d", &ar[rows][cols] );

                temp = disp_arr(ar[rows][cols]);

                printf( "%d\t", temp );
            }
        printf("\n");
    }

}/* end main */

disp_arr( int temp )
{
    int x,y;
    int a[x][y];

    printf( "%d", a[x][y] );

    return a[x][y];
}

我的错误在哪里?

最佳答案

这是一个大问题:

int x,y;
int a[x][y];

定义局部变量时,默认情况下它们不会被初始化。相反,它们的值是不确定的,并且在未初始化时使用这些值会导致未定义的行为。

您还应该收到大量编译器警告,甚至错误(例如全局范围内的 disp_arr( temp ); 函数调用)。

此外,即使未声明的函数隐含返回 int,您仍应始终指定它。

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

相关文章:

c - 为什么在调用 getc 时会出现段错误?

c++ - 前或后测试循环?

javascript - Fullcalendar with spring @ResponseBody 返回带有 406 错误 :not Acceptable Header 的 Json 数组

PHP 命名空间函数最佳实践

python - 更改导入的类变量

Javascript - 将数字变量拆分为函数

c - 在 C 中说 "#define FOO FOO"有什么意义?

文本文件的副本

c - 尝试复制固定长度的数组

php - 将字符串分解为嵌套数组