tags:

views:

542

answers:

7

Hi,

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

A: 

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

leiz
+3  A: 

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

Ed Swangren
NULL is not an integer. It is a representation of a pointer to nothing. Down this path leads to madness.
Martin York
It doesn't help that macro `NULL` is actually constant integral literal 0...
Pavel Minaev
OK, but the point is that no element in an array of structs will ever be NULL and it is not even legal to compare a struct with NULL.
Ed Swangren
@Martin: http://stackoverflow.com/questions/423823/whats-your-favorite-programmer-ignorance-pet-peeve/1331729#1331729
Johannes Schaub - litb
+15  A: 

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.

Martin
+1 Better explanation than mine
Ed Swangren
And it keeps growing!
Ed Swangren
Great answer Martin. Thanks heaps.
RWendi
If by "initialized" he meant that not all members will necessarily be created (i.e. some might be null), then adding the constructor doesn't actually solve the problem. There can still be a case in which only a part of the array is initialized.
Edan Maor
No, if you have a default constructor, and you define an array of that type, then the constructor will be called for every element of the array. Try it: with my example, but throw 'std::cout << "ctor" << std::endl;' into the constructor too. In your main, just say 'A a[10];'. You'll get "ctor" printed 10 times.
Martin
A: 

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.
ChrisW
A: 

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;
}
kitchen
A: 

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;
sbi
A: 

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
 }
};
sat
Ahhh... why? How can you meaningfully compare NULL to a struct?
Ed Swangren
This only ensure you can use operator = on your struct, Now its user term to define conditionthats why i have said:"For any reason if you have to test your struct objec against NULL"Users has to define his own condition for this,
sat