如何做阵列[100] = {0}设置整个阵列为0? [英] how does array[100] = {0} set the entire array to 0?

查看:145
本文介绍了如何做阵列[100] = {0}设置整个阵列为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器如何填写字符数组值[100] = {0}; ?什么是神奇的背后?

How does the compiler fill values in char array[100] = {0};? What's the magic behind it?

我想知道编译器如何在内部进行初始化。

I wanted to know how internally compiler initializes.

推荐答案

这不是魔术。

在C中,它code的行为在C规格(的ç规格在线征求意见稿):对于没有指定值的元素,编译器初始化指针为NULL,算术类型为零(递归适用这集合体)。

The behavior of this code in C is described in section 6.7.8.21 of the C specification (online draft of C spec): for the elements that don't have a specified value, the compiler initializes pointers to NULL and arithmetic types to zero (and recursively applies this to aggregates).

在C ++这个code的行为在C ++规范的第8.5.1.7(描述的的C网上草案++规范):编译总初始化没有指定值的元素

The behavior of this code in C++ is described in section 8.5.1.7 of the C++ specification (online draft of C++ spec): the compiler aggregate-initializes the elements that don't have a specified value.

另外,还要注意在C ++中(但不是C),您可以使用空初始化列表,从而使编译器聚合初始化所有的数组中的元素:

Also, note that in C++ (but not C), you can use an empty initializer list, causing the compiler to aggregate-initialize all of the elements of the array:

char array[100] = {};

至于什么样的$ C $的c中的编译器可能会生成执行此操作时,看看这个问题:<一href=\"http://stackoverflow.com/questions/531477/strange-assembly-from-array-0-initialization/531490\">Strange从0阵列的初始化组装

这篇关于如何做阵列[100] = {0}设置整个阵列为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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