元素乘元素"通过使用两个&QUOT初始化数组意外的输出; &安培; "指定"技术的配合 [英] Unexpected output on initializing array by using both "Element-by-Element" & "Designated" techniques together

查看:188
本文介绍了元素乘元素"通过使用两个&QUOT初始化数组意外的输出; &安培; "指定"技术的配合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99同时使用元素乘元素&放大器提供了一个功能初始化数组; 指定
方法一起:

C99 provides a feature to initialize arrays by using both element-by-element & designated method together as:

int a[] = {2,1,[3] = 5,[5] = 9,6,[8] = 4};

在运行code:

#include <stdio.h>

int main()
{
   int a[] = {2,1,[3] = 5,[0] = 9,4,[6] = 25};
   for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
          printf("%d    ",a[i]);

   return 0;
 }

注意元素0 初始化为 2 ,然后再通过初始化标志 [0]
9
我期待了元素0 (这是 2 )将由 9 (如标志 [0] = 9
因此O / P将成为

(Note that Element 0 is initialized to 2 and then again initialised by designator [0] to 9) I was expecting that element 0(which is 2) will be replaced by 9(as designator [0] = 9) and hence o/p will become

   9    1   0   5   4   0   25

不幸的是我错了,因为O / P出来;

Unfortunately I was wrong as o/p came;

   9    4   0   5   0   0   25

意外的O / P任何解释?

Any explanation for unexpected o/p?

推荐答案

与初始化的初始化数组的过程基本上是:

The process of initializing an array with an initializer is basically:


  1. 设置索引计数器为0,并初始化整个阵列到0

  2. 经过初始化元素从左至右

  3. 如果初始化元素都有指定的索引,设置索引计数器到指定的指数

  4. 的初始元素值存储在索引计数器给定的索引

  5. 递增索引计数器

  6. 返回到第3步,如果有更多的初始元素。

这篇关于元素乘元素&QUOT;通过使用两个&QUOT初始化数组意外的输出; &安培; &QUOT;指定&QUOT;技术的配合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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