简体   繁体   English

c中2d数组的5 * 1矩阵

[英]5*1 matrix of 2d array in c

when i am working with 2d arrays. 当我使用二维数组时。 i had given an input of 5*1 matrix and the it results like this. 我给了一个5 * 1矩阵的输入,其结果是这样的。

#include<stdio.h>
int main(){
int rows=5,cols=1;
int arr[rows][cols];
for(int i=0;i<rows;i++){
    for(int j=0;j<cols;j++){
        scanf("%d",&arr[i][j]);
    }
}
printf("%d\n",arr[1][1]);
printf("%d\n",arr[2][0]);
printf("%d\n",arr[0][2]);
return 0;
}

Input: 1 2 3 4 5 输入: 1 2 3 4 5

Output : 3 3 3 输出: 3 3 3

How thus it works? 它是如何工作的?

Change below snippet 在摘要下方更改

printf("%d\n",arr[1][1]);
printf("%d\n",arr[2][0]);
printf("%d\n",arr[0][2]);

to following one: 到以下一个:

printf("%d\n",arr[1][0]);
printf("%d\n",arr[2][0]);
printf("%d\n",arr[0][0]);

As Yunnosch mentioned in comment, you are trying to access outside of array and observing undefined behaviour. 正如Yunnosch在评论中提到的那样,您正在尝试访问数组之外​​的内容并观察未定义的行为。

With (effectively) 与(有效)

int arr[5][1];

The only array members you can meaningfully access are 您可以有意义地访问的唯一数组成员是

arr[0][0]
arr[1][0]
arr[2][0]
arr[3][0]
arr[4][0]

You might get lucky that your accesses 您可能会幸运地获得访问权限

arr[r][c]

with c!=0 are treated as c!= 0时被视为

arr[r+c]

but there is no guarantee. 但没有保证。

You probably have intentionally designed your access to all be r+c==2. 您可能有意地将所有访问权限设计为r + c == 2。
And the value of arr[2] is 3. 并且arr [2]的值为3。

Here 这里

int arr[rows][cols];

cols value is 1 means in each arr[rows] you can store only one elements as array index starts from zero . cols值为1表示在每个arr[rows]只能存储一个元素,因为数组索引zero Hence arr[1][1] doesn't exists at all, there is only arr[1][0] exists. 因此, arr[1][1]根本不存在,仅存在arr[1][0]

Here in all printf statement you are accessing array elements out of bounds which invokes undefined baheviour . 在这里,在所有printf语句中,您都超出了访问数组元素的范围,从而调用了undefined baheviour

printf("%d\\n",arr[1][1]);/* result is UB, it may prints some junk data */

Accessing an array subscript out of range is undefined behavior . 访问数组下标超出范围是未定义的行为

arr is a 2D array with dimension 5x1 . arr是尺寸为5x12D数组。 These statements 这些陈述

printf("%d\n",arr[1][1]); // UB --> accessing second row second column element
printf("%d\n",arr[2][0]); // Valid --> accessing third row first column elemnt
printf("%d\n",arr[0][2]); // UB --> accessing first row third column element

The in-memory view of 5x1 array would be something like this: 5x1阵列的内存视图如下所示:

arr 5x1
        col 0
 row 0  +----+
 [0][0] |    |
        |    |
 row 1  +----+
 [1][0] |    |
        |    |
 row 2  +----+
 [2][0] |    |
        |    |
 row 3  +----+
 [3][0] |    |
        |    |
 row 4  +----+
 [4][0] |    |
        |    |
        +----+

In 5x1 array, the valid value of rows is from 0 to 4 and the only valid value of column is 0 (as you have only one column in your 2D array). 5x1数组中,行的有效值为04并且列的唯一有效值为0 (因为2D数组中只有一列)。 Trying to access any value of row and column beyond these will lead to undefined behavior. 尝试访问rowcolumn之外的任何值将导致未定义的行为。

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

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