Different ways to Declare arrays in C++

In this post, we are going to learn Different ways to Declare arrays in C++. We will learn about 1-D, 2-D, and Multi-Dimensional array declaration in C++. By the end of this article, you will be able to answer the below questions:

  • How to declare 1-D array in C++.
  • How to declare 2-D array in C++.
  • How to declare 3-D array in C++.

1.How to declare 1-D array in C++


This simple and most easy way is by just specifying the name and array size in square brackets.This will declare an array of integers with size 5. First index will be [0] and the last index will be [4].

C++ Example to declare 1-D array

	int NumArray[5];

Another way is to declare and initialize at the same time. In this below line of code, we are declaring an array of size 5 and also assigning values to those elements. Please remember you can not assign more values than the size of the array itself.

C++ example to declare 1-D array

	int NumArray[5] = {0,1,2,3,4};

Sometime we can declare the array without giving the size , but that does not mean it gives us freedom to change the size at runtime.The answer is NO!!. We can avoid giving the size but in this case compiler itself counts the elements that we are assiging values to it.If we are assigning 5 values it means we want a 5 size array. Once declared it can not be modified at runtime. It can have less elements than its declared size but it any condition it can not exceed the size given declaration.

C++ example to declare 1-D array

int main()
{	
	int NumArray[] = {0,1,2,3,4};
}

2. How to declare 2-D array in C++


This simple and most easy way is by just specifying the name and array size in square brackets. This will declare an array of integers with a size 3×3. First index will be [0][0] and last index will be [2][2].

C++ example to declare 2-D array

	int NumArray[3][3];

Another way is to declare and initialize at the same time. In this below line of code, we are declaring an array of size 3×3 and also assigning values to those elements. Please remember you can not assign to any of the new index which was not declared.

C++ example to declare 2-D array

int NumArray[3][3] = {
	{0,1,2},                   /*  row at index 0 */
	{3,4,5},                   /*  row at index 1 */
	{6,7,8},                   /*  row at index 2 */
	};


Sometimes we can declare the array without giving the size, but that does not mean it gives us the freedom to change the size at runtime. The answer is NO!!. We can avoid giving the size but in this case compiler itself counts the elements that we are assigning values to it. If we are assigning 9 values it means we want a 3×3 size array. Once declared it can not be modified at runtime. It can have fewer elements than its declared size but it any condition it can not exceed the size given at declaration.

C++ example to declare 2-D array

int main()
{

	
	int NumArray[][] = {
	{0,1,2},                   /*  row at index 0 */
	{3,4,5},                   /*  row at index 1 */
	{6,7,8},                   /*  row at index 2 */
	};
}

3. How to declare 3-D array in C++.


In this example, we will learn how to declare a 2-D array in C++. This simple and most easy way is by just specifying the name and array size in square brackets. This will declare an array of integers with size (2×3) x 2. First index will be [0][0][0].

C++ example to declare 3-D array

int NumArray[2][3][2];


Another way is to declare and initialize at the same time. In this below line of code, we are declaring an array of size (2×3) x 2 and also assigning values to those elements. Please remember you can not assign to any of the new indexes which was not declared.

C++ example to declare 3-D array

int NumArray[2][3][2] = {
		{{4, 8},{2, 4},{1, 6}},
		{{3, 6},{5, 4},{9, 3}}
	};


Sometimes we can declare the array without giving the size, but that does not mean it gives us the freedom to change the size at runtime. The answer is NO!!. We can avoid giving the size but in this case compiler itself counts the elements that we are assigning values to it. If we are assigning 12 values it means we want a 12 size array. Once declared it can not be modified at runtime. It can have fewer elements than its declared size but it any condition it can not exceed the size given at declaration.

C++ example to declare 3-D array

int NumArray[][][] = {
		{{4, 8},{2, 4},{1, 6}},
		{{3, 6},{5, 4},{9, 3}}
	};