0

scanf( "%d", &ar[rows][cols] );ユーザーが int 変数に入力した値を取得しようとしていますtemp

しかし、どういうわけか実行すると、直後にエラーが発生しますprintf( "Please enter 9 positive integers : " );

編集:コードを含めるのを忘れていました。コードは次のとおりです。

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

私の間違いはどこですか?

4

3 に答える 3

1

ここに1 つの大きな問題があります。

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

ローカル変数を定義すると、デフォルトでは初期化されません。代わりに、それらの値は不確定であり、初期化されていないときにこれらの値を使用すると、未定義の動作が発生します。

また、多くのコンパイラ警告やエラー (disp_arr( temp );グローバル スコープでの関数呼び出しなど) も表示されるはずです。

また、宣言されていない関数は を返すことが暗示されていますがint、とにかく常に指定する必要があります。

于 2013-11-11T11:02:19.387 に答える
0

また、ユーザー入力と印刷を混同しないでください。そのコメントには、関数が何をすべきか、およびそのプロトタイプがどのように見えるべきかが示されています。ですから、言われたことだけを実行してください。コードからユーザー入力を削除し、既に記述したコードを関数に移動すると、次のようになります。

void disp_arr (int a[NROW][NCOL])
{
  for (int rows=0; rows<NROW; rows++)
  {
    for (int cols=0; cols<NCOL; cols++)
    {
      printf("%d ", a[rows][cols]);
    }
    printf("\n");
  }
}
于 2013-11-11T12:29:45.560 に答える