简体   繁体   English

printf根据输入在C中打印多维数组的意外的最后一个元素?

[英]printf prints unexpected last element of a multidimensional array in C, depending on input?

I'm learning to use multidimensional arrays in C and I'm unable to understand why printf() sometimes gives an unexpected result in the following program. 我正在学习在C语言中使用多维数组,无法理解为什么在以下程序中printf()有时会产生意外结果。

The idea in this program is that I wish to initialize a 5x2 array and accept 5 integers from the user with scanf to populate the 2nd index, then print the array: 该程序的想法是,我希望初始化一个5x2数组,并使用scanf从用户那里接受5个整数以填充第二个索引,然后打印该数组:

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int i=0, mat[5][2] = {
        {0, 0},
        {0, 0},
        {0, 0},
        {0, 0},
        {0, 0}
    };

    printf("Please enter 5 integers then press enter: \n");

    do {
        scanf("%i", &mat[i][2]);
        i++; 
       } while (getchar() != '\n');


    printf("Here's what the 5x2 array looks like: \n");

    for(i = 0; i < 5; i++){
        printf("%i %i", mat[i][1], mat[i][2]);
        printf(" \n");
    }

    return 0;   
}

If I enter certain integers as the user, then the output is as expected: 如果我以用户身份输入某些整数,则输出如预期的那样:

C:\Users\hackr>tmp.exe
Please enter 5 integers then press enter:
0 1 2 3 4
Here's what the 5x2 array looks like:
0 0
0 1
0 2
0 3
0 4

However, if I enter different integers, then the last line of output is not what I expected: 但是,如果输入不同的整数,则输出的最后一行不是我期望的:

C:\Users\hackr>tmp.exe
Please enter 5 integers then press enter:
1 2 3 4 5
Here's what the 5x2 array looks like:
0 1
0 2
0 3
0 4
0 4

C:\Users\hackr>tmp.exe
Please enter 5 integers then press enter:
9 8 7 6 5
Here's what the 5x2 array looks like:
0 9
0 8
0 7
0 6
0 4

In fact, as you can see above, it looks like the final element of index 2 is arbitrarily "4". 实际上,如您在上面看到的那样,索引2的最后一个元素看起来像是任意的“ 4”。

Perhaps this is due to a misunderstanding on my part regarding how the array values are indexed or referenced? 也许这是由于我对数组值如何被索引或引用的误解?

Always the array starts with the index 0. 数组始终以索引0开头。

You can try the following snippet of code: 您可以尝试以下代码段:

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int i=0, mat[5][2] = {
        {0, 0},
        {0, 0},
        {0, 0},
        {0, 0},
        {0, 0}
    };

    printf("Please enter 5 integers then press enter: \n");

    do {
        scanf("%i", &mat[i][1]);
        i++; 
       } while (getchar() != '\n');


    printf("Here's what the 5x2 array looks like: \n");

    for(i = 0; i < 5; i++){
        printf("%i %i", mat[i][0], mat[i][1]);
        printf(" \n");
    }

    return 0;   
}

while declaration you can use mat[5][2], but for access you should use 在声明时,您可以使用mat [5] [2],但是对于访问,您应该使用

mat[0][0] mat[0][1]
mat[1][0] mat[1][1]
...
..
mat[4][0] mat[4][1]

I think now it should work. 我认为现在应该可以了。

mat is defined as: mat定义为:

int mat[5][2];

Valid elements are mat[x][y] for x in range 0 ~ 4 and y in range 0 ~ 1, while you are trying to access out of bound elements in 有效元素是mat[x][y]用于x的范围为0〜4, y的范围为0〜1,而您尝试访问的范围是

scanf("%i", &mat[i][2]);

and

printf("%i %i", mat[i][1], mat[i][2]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM