在 C 中打印数组时的奇怪行为? [英] Weird behavior when printing array in C?

查看:26
本文介绍了在 C 中打印数组时的奇怪行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印一个数组,但是我没有得到所需的输出,循环完成打印预定义的数组后出现奇怪的数字.

I am trying to print an array however I am not getting the desired output, weird numbers appear after the loop finishes printing the pre-defined array.

代码是:

#include <stdio.h>    
int main(){    
    int intArray[11] = {1,2,8,12,-13,-15,20,99,32767,10,31};
    int i=0;        
    for(i=0;i<sizeof(intArray);i++){
        printf("%d\n",intArray[i]);
    }
}

输出:

1
2
8
12
-13
-15
20
99
32767
10
31
11
1629976468
2674040
2665720
1627423265
1
2665616
-2147417856
1629976534
1629976468
2674040
0
1627423172
1629976532
0
1629110043
0
0
0
0
0
0
0
0
0
0
0
1629976538
0
1629956432
2674276
0
1627407935

推荐答案

for 循环中的中断条件错误!这会导致 index-out-of bound 问题i 超过了最大索引值 10,因为数组长度仅为 11.循环中断条件应该是 <数组的长度(=11)但不是 <数组的大小.sizeof(intArray) 的值等于 11 * sizeof(int) (= 44).

The breaking condition in for loop is wrong! Which causes an index-out-of bound problem as i exceeds the maximum index value that is 10 because array length is just 11. The loop break condition should be < length of array( =11) but not < size of array. Value of sizeof(intArray) is equals to 11 * sizeof(int) (= 44).

要理解它,请阅读: sizeof Operator:

To understand it read: sizeof Operator:

当您将 sizeof 运算符应用于数组类型时,结果是数组中的总字节数.

6.5.3.4 The sizeof operator, 1125:

When you apply the sizeof operator to an array type, the result is the total number of bytes in the array.

据此当sizeof应用于静态数组标识符的名称时(不是通过malloc()/calloc()分配的)),结果是整个数组的字节大小,而不仅仅是地址.即等于每个元素的大小乘以数组的长度.
换句话说: sizeof(intArray) = 11 * sizeof(int) (因为 intArray 长度是 11 ).所以假设如果sizeof(int)是4字节那么sizeof(intArray)等于44.

According to this when sizeof is applied to the name of a static array identifier (not allocated through malloc()/calloc()), the result is the size in bytes of the whole array rather then just address. That is equals to size of each elements multiply by the length of array.
In other words: sizeof(intArray) = 11 * sizeof(int) ( as intArray length is 11 ). So suppose if sizeof(int) is 4-bytes then sizeof(intArray) is equals to 44.

下面的代码示例及其输出将帮助您进一步理解(阅读评论):

Below a code example and its output will help you to understand further(read comments):

int main(){
    int intArray[11] = {1, 2, 8, 12, -13, -15, 20, 99, 32767, 10, 31};
    int i = 0;
 
    printf("sizeof(intArray):  %d\n", 
            sizeof(intArray)                       //1. Total size of array
    ); 
    printf("sizeof(intArray[0]):  %d\n", 
            sizeof(intArray[0])                    //2. Size of one element
    ); 
    printf("length:  %d\n", 
            sizeof(intArray) / sizeof(intArray[0]) //3. Divide Size
    );    
    return 0;
}

输出:

sizeof(intArray):  44    //1. Total size of array:  11 * 4 = 44
sizeof(intArray[0]):  4  //2. Size of one element:  4
length:  11              //3. Divide Size:          44 / 4 = 11 

可以检查工作代码@ideone,注意:我假设 int 的大小是4.

One can check the working code @ideone, note: I am assuming size of int is 4.

现在注意 sizeof(intArray)44,它大于数组的长度,因此条件是错误的,你有 运行时代码中的未定义行为.要纠正它替换:

Now notice as sizeof(intArray) is 44 that is more then length of array hence the condition is wrong and you have Undefined behavior in the code at runtime. To correct it replace:

for(i=0; i < sizeof(intArray); i++)
//           ^--replace-----^
//            wrong condition = 44

与:

for(i=0; i < sizeof(intArray) / sizeof(intArray[0]); i++)
          // ^------------------------------------^
          // condition Corrected = 11 


 

为了计算数组的长度,我简单地将数组的总大小除以一个元素的大小,代码为:

To calculate length of array, I simply divided total size of array by the size of one element and code is:

sizeof(intArray) / sizeof(intArray[0])   // 44 / 4 = 11
     ^                 ^
total size of       size of first element
array   
  

这篇关于在 C 中打印数组时的奇怪行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆