A simple short array declaration which includes its definition can be declared in any scope in C++ as follows:
This is an array of characters whose name is ch. The array literal is an example of an initializer_list.
This same array can be declared and initialized as follows, in a function scope or a nested local scope but not in the global scope:
ch[0] = 'A';
ch[1] = 'B';
ch[2] = 'C';
ch[3] = 'D';
ch[4] = 'E';
If this code segment is typed in the global scope, the compiler will issue five error messages for the five assignment lines. However, the array can be declared without initialization in the global scope and then assigned values in the function scope (or other scopes), as the following program shows:
using namespace std;
char ch[5];
int main()
{
ch[0] = 'A';
ch[1] = 'B';
ch[2] = 'C';
ch[3] = 'D';
ch[4] = 'E';
return 0;
}
The global scope has the declaration “char ch[5];”. The assignment of the values has been done in the C++ main() function. The C++ main function is still a function.
These are the rules on how to use an array in the global scope, function scope and nested local scope (or any other scope):
1. An array can be declared with the initialization of practical values in one statement in any scope (global, function, nested local scope).
2. An array can be declared without initialization of practical values in the global scope and then have assigned practical values in function scope or nested local scope.
3. An array can be declared without initialization of practical values in function scope or nested local scope and have assigned practical values in that same local scope.
These rules also apply to scalar (fundamental) types. The rest of this article begins by illustrating the declaration and assignment of practical values of fundamental types in the global and the other scopes (function and local). This is followed by the illustration of the declaration and assignment of practical values of the array type in the global and the other scopes (function and local). The integer (int) is used as the example for the fundamental types. The above three rules are demonstrated for the integer and the array.
Integer Type Declaration with Global and Other Scopes
In this section, the rules are demonstrated with the integer type.
First Rule:
An integer can be declared with initialization of a practical value in one statement in any scope (global, function, nested local scope). The following program illustrates this with three different integer variables:
using namespace std;
int int1 = 1;
int main()
{
cout<< int1 <<endl;
int int2 = 2;
cout<< int2 <<endl;
if (1 == 1) {
int int3 = 3;
cout<< int3 <<endl;
}
return 0;
}
The output is:
2
3
The nested local scope (block) is the one beginning with the if-condition.
Second Rule:
An integer can be declared without initialization of a practical value in the global scope and then have assigned a practical value in function scope or nested local scope. The following program illustrates this with one integer variable:
using namespace std;
int inter;
int main()
{
cout<< inter <<endl;
inter = 20;
cout<< inter <<endl;
if (1 == 1) {
inter = 30;
cout<< inter <<endl;
}
return 0;
}
The output is:
20
30
When an integer is declared without assignment, the integer takes the default value of zero. In this case, the zero is not a practical value.
Third Rule:
An integer can be declared without initialization of a practical value in function scope or nested local scope and have assigned practical values in that same local scope. The following program illustrates this with two different integer variables:
using namespace std;
int main()
{
int int2;
int2 = 2;
cout<< int2 <<endl;
if (1 == 1) {
int int3;
int3 = 3;
cout<< int3 <<endl;
}
return 0;
}
The output is:
3
The following program will not compile, and the compiler will issue an error message:
using namespace std;
int inter;
inter = 5;
int main()
{
cout<< inter <<endl;
return 0;
}
The problem is the global scope code segment:
inter = 5;
The second statement is not allowed in this context.
Array Type Declaration with Global and Other Scopes
First Rule:
An array can be declared with the initialization of practical values in one statement in any scope (global, function, nested local scope). The following program illustrates this with three different arrays:
using namespace std;
char ch1[] = {'A', 'B', 'C', 'D', 'E'};
int main()
{
cout<< ch1 <<endl;
char ch2[] = {'F', 'G', 'H', 'I', 'J'};
cout<< ch2 <<endl;
if (1 == 1) {
char ch3[] = {'K', 'L', 'M', 'N', 'O'};
cout<< ch3 <<endl;
}
return 0;
}
The output should be:
FGHIJ
KLMNO
The nested local scope (block) is the one beginning with the if-condition.
Second Rule:
An array can be declared without initialization of practical values in the global scope and then have assigned practical values in function scope or nested local scope (or any other scope). The following program illustrates this with one array:
using namespace std;
char ch[5];
int main()
{
cout<<ch<<endl;
ch[0] = 'F';
ch[1] = 'G';
ch[2] = 'H';
ch[3] = 'I';
ch[4] = 'J';
cout<<ch<<endl;
if (1 == 1) {
ch[0] = 'K';
ch[1] = 'L';
ch[2] = 'M';
ch[3] = 'N';
ch[4] = 'O';
cout<<ch<<endl;
}
return 0;
}
The output should be:
FGHIJ
KLMNO
When a character is declared without assignment, the character takes the default value of '' (no character). In this case, the '' is not a practical value. There are five '' for the global case.
Note: The assignment can only be done this way without the initializer_list.
Note: With arrays, when the array is declared without initialization of practical values in a function or nested scope, the default values can be arbitrary. The default values are only '', for the global case. This arbitrary feature applies to integer array as well.
Third Rule:
An array can be declared without initialization of practical values in function scope or nested local scope and have assigned practical values in that same local scope. The following program illustrates this with two different arrays:
using namespace std;
int main()
{
char ch2[5];
ch2[0] = 'F';
ch2[1] = 'G';
ch2[2] = 'H';
ch2[3] = 'I';
ch2[4] = 'J';
cout<< ch2 <<endl;
if (1 == 1) {
char ch3[5];
ch3[0] = 'K';
ch3[1] = 'L';
ch3[2] = 'M';
ch3[3] = 'N';
ch3[4] = 'O';
cout<< ch3 <<endl;
}
return 0;
}
The output should be:
KLMNO
The following program will not compile, and the compiler will issue some error messages:
using namespace std;
char ch1[5];
ch1[0] = 'A';
ch1[1] = 'B';
ch1[2] = 'C';
ch1[3] = 'D';
ch1[4] = 'E';
int main()
{
cout<< ch1 <<endl;
return 0;
}
The problem is the global scope code segment:
ch1[0] = 'A';
ch1[1] = 'B';
ch1[2] = 'C';
ch1[3] = 'D';
ch1[4] = 'E';
The assignment statements are not allowed in this context.
Conclusion
These are the rules on how to use an array in the global scope, function scope and nested local scope (or any other scope):
1) An array can be declared with initialization of practical values in one statement, in any scope (global, function, nested local scope).
2) An array can be declared without initialization of practical values, in the global scope, and then have assigned practical values, in function scope or nested local scope (or any other scope).
3) An array can be declared without initialization of practical values, in function scope or nested local scope, and have assigned practical values, in that same local scope.