the trap: c++'s array

#include <iostream>

int main()
{
    using namespace std;
    int yam[3];
    yam[0] = 7;
    yam[1] = 8;
    // why it can't report the warnning? when yam[3] is existed?
    yam[3] = 6;            // yam[2] = 6 can correct the fault.

    int yamcost[3] = {20, 30, 5};

    cout << "Total yams = " << yam[0] + yam[1] + yam[2] << endl;
    cout << "The package with " << yam[1] << " yams costs "
        << yamcost[1] << " cents per yam.\n";

    int total = yam[0] * yamcost[0] + yam[1] *yamcost[1];
    total = total + yam[2] *yamcost[2];
    cout << "The total yam expense is " << total << " cents.\n";

    cout << "\nSize of yams array = " << sizeof yam << " bytes.\n";
    cout << "Size of one element = " << sizeof yam[0] << " bytes.\n";
    cout << "Size of char = " << sizeof 'c' << " bytes.\n";

    int i ;
    for(i= 0; i < 3; ++i)
        cout << yam[i] << endl;

    return 0;

}

Caution: The error of yan[3] isn't existed at all, but the compiler can't send the warning or error? It automatically arrange a value or a address... 

the best habit, the best style for design

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值