How To Declare An Empty Array in C++

Are you new to C++ and searching for how to declare an Empty Array? In this article, you will learn how to declare or initialize an Empty array in C++.

You can do this in many different ways. I will be discussing two ways in which you can declare or initialize an empty array in C++.

1. Declare An Empty Array with Fixed Size

So, for example, you might want to initialize an array with a fixed size, but you might also want to initialize the array with a limit at the start of the program.

Let us see in the below example code the declaration of the empty array with a fixed size in C++.

#include <iostream>

using namespace std;
 
int main()
{
    //Initiliazing an Integer Empty Array
    int emptyArray[10];

    cout<<emptyArray[0];

    return 0;
}

Output:

-712436000

As you can see in the above code, I haven’t assigned any value to the empty array and all I have done is to declare the Empty array with 10 integer elements. In C++, if you try to print or generate an output of the empty array then the compiler assigns some random value to each element of the array if not assigned by you to avoid runtime error.

2. Declare An Empty Array with Fixed Size and Value

Now if you want to initialize the array with some fixed size and to a specific value to overcome the above mentioned problem. As in this case, all the elements will be assigned to number zero instead of some random value assigned by the C++ interpreter then you can use the below code for example.

#include <iostream>

using namespace std;
 
int main()
{
    //Initiliazing an Integer Empty Array
    int emptyArray[10] = {0};

    cout<<emptyArray[0]<<" "<<emptyArray[1];

    return 0;
}

Output:

0 0

As you can see in the above code I was able to assign the values to all the elements in the empty array with value zero. And to verify that you have correctly assigned the values you can try to print the empty array that is declared as in this case the output was zero for all the elements.

How To Declare An Empty Array in C++

Wrap Up

I hope you got your answer related to how to declare an empty array in C++. I have suggested the two methods that you can use to declare or initialize an empty array. One method is without assigning any fixed values to the elements of the array and another method is assigning fixed values to elements of an array while they are declared or initialized.

Let me know in the below comment section if you found any better ideas than the one discussed above I will be happy to share them here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Convert Char to Int in C++
  2. How To Find The Sum Of An Array Of Numbers in JavaScript
  3. Easiest and Best Way To Convert Int to String in C++
  4. How To Convert a Char to String in C++ Quickly
  5. Parse (split) or Tokenize a String in C++ Using Delimiter
  6. Different Ways to Sort a Vector In C++
  7. std::stoi Convert String To Numbers in C++ [3 Ways]
  8. How To Pop Front in Vector C++ [pop_front]
  9. How To Square Something in C++? 3 Unique Ways
  10. Iterate Through Character Of String in C++
How To Declare An Empty Array in C++

Leave a Comment