arrays....

Feb 27, 2011 at 1:44am
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
Feb 27, 2011 at 1:59am
Define empty.
Feb 27, 2011 at 2:01am
i have the array

int array[10];

but nothing else is done to it till the:

if(array[index] == ?Empty?)then do this
Feb 27, 2011 at 2:21am
A memory location cannot be empty. If you tried outputting that array index it will always return something.
Last edited on Feb 27, 2011 at 2:21am
Feb 27, 2011 at 2:34am
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.
Feb 27, 2011 at 2:40am
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.
Feb 27, 2011 at 2:41am
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
Feb 27, 2011 at 2:49am
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 Feb 27, 2011 at 2:53am
Feb 27, 2011 at 3:05am
i think i will used the -1 suggestion, thanks filipe
Feb 27, 2011 at 3:21am
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.
Feb 27, 2011 at 4:12am
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 Feb 27, 2011 at 4:16am
Feb 27, 2011 at 11:21am
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.