// --- Example: Initializing 1-dim. arrays -- // // Illustrates various methods of array initialization // and what happens when they're not done right. // Also shows how character and numeric arrays // are handled differently by the << operator. // ----------------------------------------------- #include using namespace std; int main() { int a_i[5] = {10,11,12,13,14,15,16}; // Never do this. (Usually // generates a compiler warning.) int a_i1[] = {10,11,12,13,14,15,16}; // This way is fine. char a_c[5] = {'a','p','p','l','e'}; char str_bad[5] = "apple"; // Generates a compiler warning -- // may or may not be correctly terminated. char str_good[] = "apple"; // OK. char str_wordy[] = "The apple of my eye."; // OK. cout << "\n\nAddress &a_i[0]: " << a_i; // The address &a_i[0] is printed // because a_i is numeric. cout << "\n\nIntegers in array a_i: "; for (int i=0; i<5; ++i) cout << a_i[i] << ' '; cout << "\n\n"; cout <<"Array: a_c: " << a_c << "\n\n"; //NOTE: displays a_c[0],...,a_c[4], // NOT the address &a_c[0]. cout <<"Bad string: " << str_bad << "\n\n"; // cout <<"Good string: " << str_good << "\n\n"; // cout <<"Wordy string: " << str_wordy << "\n\n"; // return 0; } // =============================================== // OUTPUT: // =============================================== /* Address &a_i[0]: 0xf7fff980 Integers in array a_i: 10 11 12 13 14 Array: a_c: apple Bad string: apple Good string: apple Wordy string: The apple of my eye. */