在函数内部调用数组

时间:2013-11-11 13:22:38

标签: c arrays function

我正在尝试调用temp[z]中存储的值,然后使用函数显示它。这是代码:

/* 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( int temp );

int main(void)
{
    /* declare needed variables or constants, e.g. */
    int ar[NROW][NCOL];
    int rows, cols;
    int z = 0;
    int temp[z] = {1,2,3,4,5,6,7,8,9}; /* Storing 9 numbers */

    /* 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] );

                /* Store values in the temp[z] = {1 2 3 4 5 6 7 8 9}*/
                temp[z] = ar[rows][cols];

                z += 1; /* Increase the array in temp[z] */
            }
        printf("\n");
    }

    /* Call disp_arr to display the 3 x 3 board */
    disp_arr( temp[z] );



}/* end main */

disp_arr( int storedValue )
{
    int x,y;
    for (  x = 0 ; x < 3 ; x++ )
    {
        for (  y = 0 ; y < 3 ; y++ )
        {
            printf( "%d\t", storedValue );
        }
        printf("\n%d");
    }


    return storedValue;
}

当我尝试执行代码时,我的编译器会给出附件中显示的错误。我想代码int temp[z] = {1,2,3,4,5,6,7,8,9};就是它的来源。

任何人都可以给我一些指示吗?

3 个答案:

答案 0 :(得分:1)

int temp[z]正在尝试创建VLAC89不支持可变长度数组。您的编译器可能不支持VLA - 请查看手册。另一个问题是您的数组大小错误int z = 0;。由此你得到警告:

  

数组初始值设定项中的多余元素

因为您已将z初始化为0,因此您的数组包含9个元素。您应该z初始化为至少9。此外,你在循环中走出界限

 temp[z] = ar[rows][cols]; // ---> z goes out of bounds here 

在循环开始之前将z初始化为0并检查数组边界。另外,printf("\n%d");需要参数,例如printf("\n%d", someint); - 如果您只想打印新行,请删除%d,如printf("\n");

答案 1 :(得分:0)

您的代码中存在非常基本的错误

1.int temp [z] = {1,2,3,4,5,6,7,8,9};应该是

int temp [9]

2.disp_arr(int storedValue)

disp_arr(int * storedValue)

3.disp_arr(temp [z]);

disp_arr(温度)

4.printf(“请输入9个正整数:”);应该在嵌套for循环之前移动到

答案 2 :(得分:0)

在您尝试的主要代码中 1)创建一个大小为零的数组,因为z = 0 2)int z应该是常量,以便它可以用于创建数组

我认为这样做会

    int ar[NROW][NCOL];
    int rows, cols;
    const int z = 9; 
    //int temp[z] = {1,2,3,4,5,6,7,8,9}; /* Storing 9 numbers */
    //commented the above line and changed the code
    int temp[z] ;
    memset( temp, 0, z*sizeof(int) );

    /* prompt for the user to enter nine positive integers to be stored into the array */
    int index = 0;
    for ( rows = 0 ; rows < 3 ; rows++ )
    {
        for ( cols = 0 ; cols < 3 ; cols++ )
            {
                printf(  "Please enter 9 positive integers : " );

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

                /* Store values in the temp[z] = {1 2 3 4 5 6 7 8 9}*/
                temp[index] = ar[rows][cols];

                index += 1; /* Increase the array in temp[z] */
            }
        printf("\n");
    }

    /* Call disp_arr to display the 3 x 3 board */
    disp_arr( temp[z] );
相关问题