RWendi
RWendi

Reputation: 1422

How to check for NULL in c++?

Let say for example, I have a struct, and an array of the struct. What I want to do is to iterate through the array and check if any of the item is a null. I tried checking the item against NULL and (struct *) 0, that don't seem to work. Is there any reliable way to check for null value?

UPDATE Sample Code

struct Test{
 int a;
};

Test testArray[size];

for (int i = 0; i < testCount; i++)
{
    if (testArray[i] == NULL) //this doesnt work
    {
    }
}

Thanks,

RWendi

Upvotes: 2

Views: 21629

Answers (7)

Satbir
Satbir

Reputation: 6506

For any reason if you have to test your struct objec against NULL

You can overload operator ==

struct Test
{
    int a;
    bool operator==(const Test const * arobj)
    {
        return true;// true/false on your condition
    }
};

Upvotes: 0

sbi
sbi

Reputation: 224069

Why don't you use a std::vector?

std::vector<Test> my_list
Test obj;

my_list.push_back(obj);
std::cout << my_list.size();
my_list[0].a = 42;

my_list.push_back(obj);
std::cout << my_list.size();
my_list[my_list.size()-1].a = 4711;

Upvotes: 0

Martin
Martin

Reputation: 3753

If you have an array of structs, then there are no pointers, so it doesn't make sense to check for null.

An array of n structs is literally n of those structs laid out one after the other in memory.

Could you change it to an array of pointers to structs, initialise them all to NULL, and create them as you need them? I'd be looking into why you want to check for null though first - it feels like you're doing something wrong.

(Edit for code added to question)

You don't show how you define testArray, but I suspect you've done the following:

Test testArray[testCount];

If you really need to compare against null, try the following:

Test *testArray[testCount];
for (int i = 0; i < testCount; i++)
{
    testArray[i] = new Test;
}

At this point, none of the entries will be NULL, but if later in your code you do something like:

testArray[3] = NULL;

then you'll be able to detect that with the loop you provided in your question.

Can you clarify why you want to compare with NULL?

(And it keeps growing again - this time to address question regarding initialisation):

As others have mentioned, comparing with NULL is not a valid way of telling if something is initialised or not. Probably the best way forward is to write your code in such a way that you guarantee it will be initialised.

Probably the best way to do this is to put a default constructor on your Test struct (you can do this in C++, but not C). Something like:

struct A {
    int x;
    A() : x(0) { }
};

will do the job just fine. Now all of the structs in your array are guaranteed to exist, and be initialised with their x value set to 0.

Upvotes: 19

xian
xian

Reputation: 4693

Your compiler is probably setting the values to some magic number while you debug.

For example, if you run the following in Visual Studio 2008 and compile in Debug mode and set a break point at the 'return 0;' statement, 'foo' points to 0xCCCCCCCC.

struct Foo {
    int Bar;
};

int main(int argc, char **argv) {
    Foo *foo; // foo points to 0xCCCCCCCC
    return 0;
}

Upvotes: 0

ChrisW
ChrisW

Reputation: 56113

You can test whether a pointer is null by comparing to 0, or by using the boolean operators like ...

if (ptr)
{
  //not null
}

if (!ptr)
{
  //null
}

if (ptr == 0)
{
  //null
}

if (ptr != 0)
{
  //not null
}

If you have an array of pointers, remember that you would need to have previously explicitly initialized any unassigned elements (pointers) to null: if you haven't initialized them, they'll contain random (non-null) values:

const char* array[5]; //an array of 5 pointers
array[0] = "Hello";
array[1] = "World";
//at this moment, array[2] hasn't been initialized: it's value
//is therefore unknown/undefined/random, not necessarily null.

Upvotes: 3

Ed Swangren
Ed Swangren

Reputation: 124642

I don't understand; if you have an array of structs, how can any of the positions be NULL? NULL is an integer.

Upvotes: 4

leiz
leiz

Reputation: 4042

If you have array of struct pointers, then you can check for null.

Upvotes: 0

Related Questions