c - 数组中的函数未显示正确的值?

标签 c arrays function

程序假设以 3 x 3 板的方式显示用户输入的 9 个数字。然而我得到的只是一些相当奇怪的数字。

我的 disp_arr 函数有问题,但我只是不知道那里有什么错误。我对函数和指针相当陌生,但我想这就是我学习的方式!

这是我运行时得到的输出:2686636 clone errors

/* 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 temp[9] = {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 */
    int index = 0;
    printf(  "Please enter 9 positive integers :\n");
    for ( rows = 0 ; rows < 3 ; rows++ )
    {
        for ( cols = 0 ; cols < 3 ; cols++ )
            {
                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] */
            }
    }

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

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

我考虑过包含像 storedValue[counter] 这样的计数器,但它返回更多错误 =/

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

                counter += 1;
            }
            printf("\n");
        }
    }

如有任何帮助,我们将不胜感激。

提前致谢!

山姆

/* Editted code after haccks and Rohan's advice */
#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 temp[9] = {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 */
    int index = 0;
    printf(  "Please enter 9 positive integers :\n");
    for ( rows = 0 ; rows < 3 ; rows++ )
    {
        for ( cols = 0 ; cols < 3 ; cols++ )
            {
                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] */
            }
    }

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

}/* end main */

disp_arr( int storedValue )
    {
        int x,y;
        int counter = 0;
        for (  x = 0 ; x < 3 ; x++ )
        {
            for (  y = 0 ; y < 3 ; y++ )
            {
                /* How on earth do I include the counter inside without errors??*/
                printf( "%d\t", storedValue );

                counter += 1;
            }
            printf("\n");
        }
    }

最佳答案

通过将 temp 传递给 disp_array 意味着您正在传递数组 temp 第一个元素的地址(因为它会衰减到在这种情况下,指针)指向您的函数。为此,函数参数的类型必须为 int *

将您的 disp_array 声明为

void disp_arr( int *temp);   

称其为

disp_array(temp);  

并将函数定义更改为

void disp_arr( int *storedValue )
{
    int i;
    for (  i = 0 ; i < NROW*NCOL ; i++ )
    {
            printf( "%d\t", storedValue[i] );
                printf("\n");
    }
}

关于c - 数组中的函数未显示正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910185/

相关文章:

c - Scanf() 无法检测到错误的输入

C union 数组未被修改

c++ - 如果我已经创建了结构数组初始化的构造函数,通过什么调用方法?

JavaScript 在函数完成之前继续!? Ajax异步混淆及解决方法

c - C 结构中奇怪的 const char 指针成员初始化

c - 如何在不调用 GetQueuedCompletionStatus() 的情况下知道 WSASend() 操作是否已完成?

arrays - 使用Python计算行是否包含这个和那个,然后制作结果的热图(?不确定这是否是正确的术语)

c - 多维数组和指针

python - 检查函数是否是某个对象的方法

javascript - for循环中变量名可以由数组值决定吗?