PDA

View Full Version : c++ help (again)



wo1olf
February 2nd, 2007, 07:57 PM
How can it test if an array is empty at a given position (index) ?

like in this way



if(array[i] != ...)
{

}

Yeldarb
February 2nd, 2007, 10:42 PM
if(array[i] != null) {
}

MTsoul
February 3rd, 2007, 12:28 AM
Are you sure about that Yeldarb? Are all array entries initialized to null before assginment?

kopo.fett
February 3rd, 2007, 10:14 PM
for (int i = 0; i < arrayName.Length; i++)
{
if (arrayName[i] != null) {
// array element is not empty
}
else {
// array element is empty
}
}

Yeldarb
February 4th, 2007, 12:37 AM
Are you sure about that Yeldarb? Are all array entries initialized to null before assginment?
No, I don't think they are (I seem to remember having uninitialized variable with arbitrary left-over bits in them but it's been a while).

But if they do have arbitrary bits then they are assigned, aren't they? There's no real way to tell whether you assigned them or they're left overs.

Best bet is to manually go through and initialize them to null I'd say.

TheColonial
April 8th, 2007, 06:05 AM
Dont forget that 'NULL' is actually defined as 0 on most platforms. Does 0 always mean empty? :) I don't think so! An array of integers can easily contain zero, that doesn't mean it's empty.

It's up to you to keep track of what's used and what's not.

Cheers
OJ