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