Professional C__ - Marc Gregoire [10]
char ticTacToeBoard[3][3];
ticTacToeBoard[1][1] = 'o';
Figure 1-1 shows a visual representation of this board with the position of each square.
FIGURE 1-1
In C++, the first element of an array is always at position 0, not position 1! The last position of the array is always the size of the array minus 1!
std::array
C++11 introduces a new type of container called std::array, defined in the The following example demonstrates how to use the std::array container. #include #include using namespace std; int main() { array cout << "Array size = " << arr.size() << endl; for (auto i : arr) cout << i << endl; cout << *iter << endl; return 0; } Code snippet from std_array\std_array.cpp Both the standard arrays and the new std::arrays have a fixed size, which should be known at compile time. They cannot grow or shrink at run time. Functions For programs of any significant size, placing all the code inside of main() is unmanageable. To make programs easy to understand, you need to break up, or decompose, code into concise functions. In C++, you first declare a function to make it available for other code to use. If the function is used inside a particular file of code, you generally declare and define the function in the source file. If the function is for use by other modules or files, you generally put the declaration in a header file and the definition in a source file. Function declarations are often called “function prototypes” or “signatures” to emphasize that they represent how the function can be accessed, but not the code behind it. A function declaration is shown below. This example has a return type of void, indicating that the function does not provide a result to the caller. The caller must provide two arguments for the function to work with — an integer and a character. void myFunction(int i, char c); Without an actual definition to match this function declaration, the link stage of the compilation process will fail because code that makes use of the function myFunction() will be calling nonexistent code. The following definition prints the values of the two parameters. void myFunction(int i, char c) { std::cout << "the value of i is " << i << std::endl; std::cout << "the value of c is " << c << std::endl; } Elsewhere in the program, you can make calls to myFunction() and pass in constants or variables for the two parameters. Some sample function calls are shown here: myFunction(8, 'a'); myFunction(someInt, 'b'); myFunction(5, someChar); In C++, unlike C, a