Search code examples
c++arraysdynamicmemory-managementdouble-pointer

How to check an empty space (NULL) in double pointers


I am trying to use an 'if' condition to check whether a location for an object is empty or not. I am using double pointers for this. Please see the code below:

void show_room :: buy_car(int clmn, int row)
{
    int row = row;
    int clmn = clmn;
    string car_door_col;
    string temp1;
    float temp2;
    int temp3;
    int n=0, i;
    this->HBM = new show_room*[3];

    for(int i=0; i<3; i++)
    {               
        this->HBM[i] = new show_room[4];
    }

    for(int j=0; j<row; j++)
    {
        if(this->HBM[clmn][row] == NULL)
        {
        }
    }
}

In this loop:

for(int j=0; j<row; j++)
{
    if(this->HBM[clmn][row] == NULL)
    {

    }
}

I am trying to check whether there is an object at [clmn][row] or not, and I am using NULL for this which is wrong. How am I supposed to implement this. Please help me in this regard.


Solution

  • Assuming you've already initialized the elements of HBM to 0, and you're remembering to reset them to 0 after deleting the objects they point to, you can simply do:

    if(this->HBM[clmn][row]) {
        // ... 
    }