arrays....

i am doing an if statement that asks if an array spot is empty. not 0 but empty. i tried NULL but thats not doing it. thanks for the help
Define empty.
i have the array

int array[10];

but nothing else is done to it till the:

if(array[index] == ?Empty?)then do this
A memory location cannot be empty. If you tried outputting that array index it will always return something.
Last edited on
closed account (zb0S216C)
int array[ 10 ];

This is an uninitialized array. All elements will store garbage until initialized.

A memory location cannot be empty

This is true.

that asks if an array spot is empty

By this, do you mean: If the value assigned to the element n in the given array is zero then...?

i tried NULL but thats not doing it
NULL can only be assigned to a pointer.
As exposed above, an array of ints can't have "empty" elements. However, you can always determine a convention if you want to. For instance, if you're only interested in positive integers, you could use -1 to signal an "empty" slot (maybe define a const int empty = -1). Be sure to document this kind of thing well, though, because it's quite non obvious.
i know but it is going to store integers and someone might add 0 to the array so i am looking for a way around it
In C++, NULL is 0. Literally. This means it can even be assigned to an int or anywhere a 0 is acceptable. Conventionally, it's only used with pointers.
Last edited on
i think i will used the -1 suggestion, thanks filipe
closed account (zb0S216C)
The deceleration of NULL is the following( if you're interested ):
 
#define NULL( ( void * )0 )    // This is it I believe. 


In C++, NULL is 0. Literally. This means it can even be assigned to an int or anywhere a 0 is acceptable.

While this is true, it could be misleading to others thinking that the variable they're working with is a pointer. So, only using NULL with hanging pointers is the smart thing to do.
No, that's not the declaration of NULL in C++. That's C. In C++, it resolves directly to 0. Here's what's in stddef.h (GCC):

1
2
3
4
#ifndef __cplusplus
#define NULL ((void *)0)
#else   /* C++ */
#define NULL 0 


it could be misleading to others thinking that the variable they're working with is a pointer.

Agreed.

So, only using NULL with hanging pointers is the smart thing to do.

Often, if you have a function that allocates some memory and returns a pointer to it, you'll want it to return NULL if it fails to allocate (or something else goes wrong).
Last edited on
In what situation do you want the array to be empty? Most of the time if you want half of the array to be empty you just wont use that many allocated addresses example, instead of using [10] you would use [5].
Topic archived. No new replies allowed.