Check if a variable is an array or not using C++

In this tutorial, we will learn how to check if a variable is an array or not in C++.

An array contains a collection of the same type of variables. The array is created for example-

int a[]={1,2,3};

Here the size of the array is different from the single variable. So, it is easy to find whether a variable is an array or not.

How to check if a variable is an array or not in C++

  1. First getting input an array.
  2. Then we are going to check whether a variable is an array or not.

Code:

#include <iostream>

using namespace std;

int main()
{
    int a[]={1,2,3,4,5,6,7,8,9};
        if(sizeof(a)==4)
            cout<<"\nvariable is not an array";
        else
            cout<<"\nvariable is an array";

    return 0;
}

 

An array has been created and values are stored in an array. Then we can check if the variable is an array or not. We are going to check the size of the variable. If it’s size equal to a single variable then it is not an array.

Output:

variable is an array

Leave a Reply

Your email address will not be published.