Posted By

marwac6 on 02/21/11


Tagged

array compatibility workaround initialisation


Versions (?)

Array


 / Published in: C++
 

Current versions of C++, as well as ANSI C, allow you to initialize ordinary arrays defined in a function. However, in some older implementations that use a C++ translator instead of a true compiler, the C++ translator creates C code for a C compiler that is not fully ANSI C compliant. In such a case, you can get an error message like the following example from a Sun C++ 2.0 system

  1. #include <iostream> int main() {
  2. using namespace std; int yams[3]; yams[0] = 7;
  3. yams[1] = 8; yams[2] = 6;
  4. int yamcosts[3] = {20, 30, 5}; // create, initialize array // NOTE: If your C++ compiler or translator can’t initialize // this array, use static int yamcosts[3] instead of // int yamcosts[3]
  5. // creates array with three elements // assign value to first element
  6. 111cout << “Total yams = “;
  7. cout << yams[0] + yams[1] + yams[2] << endl; cout << “The package with “ << yams[1] << “ yams costs “; cout << yamcosts[1] << “ cents per yam.\n”; int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1]; total = total + yams[2] * yamcosts[2];
  8. cout << “The total yam expense is “ << total << “ cents.\n”;
  9. cout << “\nSize of yams array =<< sizeof yams; cout << “ bytes.\n”;
  10. cout << “Size of one element =<< sizeof yams[0]; cout << “ bytes.\n”; return 0;
  11. }

Report this snippet  

You need to login to post a comment.