1. Un - sized Array Initialization

Imagine that you are using array initialization to build a table of error messages, as shown here:
char e1[14] = "Divide by 0\n";
char e2[23] = "End-of-File\n";
char e3[21] = "Access Denied\n";
 As you might guess, it is very tedious to manually count the characters in each message to determine the correct array dimension. It is possible to let C++ automatically dimension the arrays in this example through the use of unsized arrays. If an array initialization statement does not specify the size of the array, then C++ will automatically create an array large enough to hold all the initializers present. When this approach is used, the message table becomes
char e1[] = "Divide by 0\n";
char e2[] = "End-of-File\n";
char e3[] = "Access Denied\n";
Besides being less tedious, the unsized array-initialization method allows you to change any of the messages without having to resize the array. This avoids errors caused by accidentally miscounting the number of characters in the message. Unsized array initializations are not restricted to one-dimensional arrays. For a multidimensional array, you must specify all but the leftmost dimension so that C++ can index the array properly. Using unsized array initializations, you can build tables of varying lengths, with the compiler automatically allocating enough storage for them.
In the following example, sqrs is declared as an unsized array:
int sqrs[][2] = {
1, 1,
2, 4,
3, 9,
4, 16,
5, 25,
6, 36,
7, 49,
8, 64,
9, 81,
10, 100
The advantage to this declaration over the sized version is that the table may be lengthened or shortened without changing the array dimensions.
Share on Google Plus