【问题标题】:Functions in array not displaying the correct values?数组中的函数未显示正确的值?
【发布时间】:2013-11-11 15:39:53
【问题描述】:

该程序假设以 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");
        }
    }

【问题讨论】:

    标签: c arrays function


    【解决方案1】:

    通过将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");
        }
    }
    

    【讨论】:

    • 嘿,很高兴再次收到您的来信!回到代码。当我在前面包含 void 时,它会给出一个错误:/ 它打印出的唯一数字是我输入的第一个数字。有什么想法吗?
    • 谢谢!不过我还有一些问题。如果要将数组传递给函数/衰减为指针,则全局范围函数和函数定义参数必须包含 *(pointer)?衰减过程是否只适用于数组?
    • 是的。在大多数但不是所有上下文中,任何数组类型的表达式都会隐式转换为(“衰减”为)指向数组对象第一个元素的指针,除非它是一元 &amp;sizeof 运算符的操作数。
    • 还有其他问题吗? :)
    • 哦,我明白了!就多了两个。您提到数组的对象第一个元素被衰减为指针。后面的元素呢?其次,术语void 的意思是“不返回任何值”。是否需要将代码void放在全局作用域函数和函数定义的前面?我听过我的讲师,但我似乎不明白..
    【解决方案2】:

    disp_arr() 应该采用int * 参数而不是int

    并像在第二个函数中一样打印它。

    printf( "%d\t", storedValue[counter] );
    

    【讨论】:

    • 当我在第二个函数中打印它时遇到大量错误。我编辑了主要代码。请看看山姆
    【解决方案3】:

    与您的功能问题没有直接关系,但我认为您可以清理一些主 for 循环。因为看起来你对你的 ar 数组什么都不做,你可以做类似下面的事情(仔细检查我是否有语法错误,因为我现在不在编译器,但这个想法应该很清楚):

    int main(void)
    {
        /*declare needed variables or constants, e.g. */
        int total = NROW * NCOL;
        int temp[total];
        printf("Please enter %d positive integers :\n", total);
        for(int i = 0; i < total; i++)
        {
            scanf("%d", temp[i]);
        }
    
        // Print the array.
        disp_arr(temp);
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2019-11-26
      • 2018-03-30
      相关资源
      最近更新 更多