打印怪异的行为,当阵列中的C 2 [英] Weird behavior when printing array in C?

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

问题描述

我想打印一个数组但是我没有得到期望的输出,奇异数出现环完成打印pre-定义的数组后。

code是:

 的#include<&stdio.h中GT;
诠释主(){
    INT intArray [11] = {1,2,8,12,-13,-15,20,99,32767,10,31};
    INT I = 0;
    对于(i = 0; I<的sizeof(intArray);我++){
        的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


解决方案

断裂条件循环是错误的!这会导致索引出界的问题的如 I 超过最大索引值 10 因为数组长度刚好 11 。该回路断线状态应该是<阵列(= 11 ),但不可以&LT的长度;阵的大小。
价值的sizeof(intArray)是等于 11 * sizeof的(INT)(= 44 )。

要了解阅读: 的sizeof 操作员


  

6.5.3.4 sizeof操作符,1125:


  
  

在应用的sizeof 运营商数组类型,其结果是字节数组中的总数。


根据这个当的sizeof 应用于静态数组标识符(通过未分配的malloc()的名称 / 释放calloc()),其结果是在整个阵列的字节而并非只是解决大小。即等于乘以每个元件由阵列的长度的尺寸。结果,
换句话说:的sizeof(intArray) = 11 * sizeof的(INT)(如 intArray 长度为11)。所以的假设的如的sizeof(INT)然后4个字节的sizeof(intArray)是等于 44

下面以一个code为例,其输出将帮助您进一步了解(阅读评论)

  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。阵列的总大小
    );
    的printf(的sizeof(intArray [0]):%d个\\ N,
            的sizeof(intArray [0])// 2。一个元件的大小
    );
    的printf(长数:%d \\ n,
            的sizeof(intArray)/的sizeof(intArray [0])// 3。鸿沟大小
    );
    返回0;
}

输出:

 的sizeof(intArray):44 // 1。阵列的总大小:11 * 4 = 44
的sizeof(intArray [0]):4 // 2。一个元件的大小:4
长度:11 // 3。分割尺寸:43/4 = 11

<子>一个可以检查工作code @ ideone ,注:我假设的大小int是 4

现在看到的的sizeof(intArray) 44 的更多的则是数组的长度,因此条件是错误的你必须在运行时的code 未定义行为。要纠正它替换:

 为(i = 0; I&LT;的sizeof(intArray);我++)
// ^ - 更换----- ^
//错误条件= 44

使用:

 为(i = 0; I&LT;的sizeof(intArray)/ sizeof的(intArray [0]);我++)
          // ^ ^ ------------------------------------
          //条件修正= 11

要计算阵列的长度,我只是一个元素和code的大小分成数组的总大小是:

 的sizeof(intArray)/ sizeof的(intArray [0])//4分之44= 11
     ^^
第一元件的尺寸的总尺寸
排列

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.

Code is:

#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]);
    }
}

Output:

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

解决方案

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).

To understand it read: sizeof Operator:

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.

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;
}

Output:

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 

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

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

With:

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 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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