当我将二维数组作为一维数组读取时会显示什么?

时间:2021-06-22 12:33:22

I am trying to understand the way that C handles arrays, in this case, by reading a two-dimensional array as though it were a one-dimensional array.

我试图理解C处理数组的方式,在这种情况下,通过读取二维数组就好像它是一维数组一样。

Given this simple C program

鉴于这个简单的C程序

#include <stdio.h>

int main(int argc, char *argv[]){

    int array[4][4];
    int i, j;

    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            array[i][j] = (i+1)*(j+1);
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }

    for(i = 0; i < 16; i++){
        printf("%d ", array[i]);    
    }

    printf("\n");

}

I get this strange output.

我得到了这个奇怪的输出。

1 2 3 4 
2 4 6 8 
3 6 9 12 
4 8 12 16 
56319776 56319792 56319808 56319824 56319840 56319856 56319872 56319888 56319904 56319920 56319936 56319952 56319968 56319984 56320000 56320016 

What is being printed in the second for loop?

在第二个for循环中打印什么?

2 个解决方案

#1


4  

What is being printed in the second for loop?

在第二个for循环中打印什么?

Short answer is "it's garbage". The official name for it is "undefined behavior", but it's a essentially you see a sequence of arbitrary decimal digits.

简短的回答是“这是垃圾”。它的正式名称是“未定义的行为”,但它基本上是一个任意十进制数字的序列。

Long answer is a little trickier: you are passing printf addresses of one-dimension arrays, which get re-interpreted as integer numbers. Note how the numbers are apart by the same step of 16. This is the size of four ints on your system.

长答案有点棘手:你传递的是一维数组的printf地址,它被重新解释为整数。请注意数字是如何分开16的相同步骤。这是系统上四个整数的大小。

If you want to get the original numbers through an array of one dimension, you could force a different re-interpretation of the array - as a pointer to int:

如果你想通过一个维度的数组得到原始数字,你可以强制对数组进行不同的重新解释 - 作为指向int的指针:

int *ptr = (int*)&array;
for(i = 0; i < 16; i++){
    printf("%d ", ptr[i]);
}

This produces the sequence of numbers from your 2D array, the way the array is stored in memory (row-by-row).

这将生成2D数组中的数字序列,即数组存储在内存中的方式(逐行)。

Demo on ideone.

在ideone上演示。

#2


4  

2D arrays are thought as 1D array of 1D arrays. It is printing the address of 1D arrays but you should use %p specifier to print the address otherwise it invokes undefined behavior.

2D阵列被认为是一维阵列的一维阵列。它打印1D数组的地址但你应该使用%p说明符来打印地址,否则它会调用未定义的行为。

for(i = 0; i < 4; i++){
    printf("%p", (void *)array[i]);    
}  

int array[4][4] is of type 2D array of 16 integers or you can say that array is a 1D array of 4 1D arrays of int's.

int array [4] [4]是16个整数的2D数组,或者你可以说数组是4个1D数组的1D数组。

You should note that only your program invokes undefined behavior for two reasons:
1. wrong specifier %d is used to print address.
2. you are accessing out of bound in your last loop. for(i = 0; i < 16; i++) should be for(i = 0; i < 4; i++)

您应该注意,只有您的程序调用未定义的行为有两个原因:1。错误的说明符%d用于打印地址。 2.你在最后一个循环中访问越界。 for(i = 0; i <16; i ++)应为(i = 0; i <4; i ++)

#1


4  

What is being printed in the second for loop?

在第二个for循环中打印什么?

Short answer is "it's garbage". The official name for it is "undefined behavior", but it's a essentially you see a sequence of arbitrary decimal digits.

简短的回答是“这是垃圾”。它的正式名称是“未定义的行为”,但它基本上是一个任意十进制数字的序列。

Long answer is a little trickier: you are passing printf addresses of one-dimension arrays, which get re-interpreted as integer numbers. Note how the numbers are apart by the same step of 16. This is the size of four ints on your system.

长答案有点棘手:你传递的是一维数组的printf地址,它被重新解释为整数。请注意数字是如何分开16的相同步骤。这是系统上四个整数的大小。

If you want to get the original numbers through an array of one dimension, you could force a different re-interpretation of the array - as a pointer to int:

如果你想通过一个维度的数组得到原始数字,你可以强制对数组进行不同的重新解释 - 作为指向int的指针:

int *ptr = (int*)&array;
for(i = 0; i < 16; i++){
    printf("%d ", ptr[i]);
}

This produces the sequence of numbers from your 2D array, the way the array is stored in memory (row-by-row).

这将生成2D数组中的数字序列,即数组存储在内存中的方式(逐行)。

Demo on ideone.

在ideone上演示。

#2


4  

2D arrays are thought as 1D array of 1D arrays. It is printing the address of 1D arrays but you should use %p specifier to print the address otherwise it invokes undefined behavior.

2D阵列被认为是一维阵列的一维阵列。它打印1D数组的地址但你应该使用%p说明符来打印地址,否则它会调用未定义的行为。

for(i = 0; i < 4; i++){
    printf("%p", (void *)array[i]);    
}  

int array[4][4] is of type 2D array of 16 integers or you can say that array is a 1D array of 4 1D arrays of int's.

int array [4] [4]是16个整数的2D数组,或者你可以说数组是4个1D数组的1D数组。

You should note that only your program invokes undefined behavior for two reasons:
1. wrong specifier %d is used to print address.
2. you are accessing out of bound in your last loop. for(i = 0; i < 16; i++) should be for(i = 0; i < 4; i++)

您应该注意,只有您的程序调用未定义的行为有两个原因:1。错误的说明符%d用于打印地址。 2.你在最后一个循环中访问越界。 for(i = 0; i <16; i ++)应为(i = 0; i <4; i ++)