13

I have an array which might contain empty/null positions (e.g: array[2]=3, array[4]=empty/unassigned). I want to check in a loop whether the array position is null.

array[4]==NULL //this doesn't work

I'm pretty new to C++.
Thanks.


Edit: Here's more code; A header file contains the following declaration

int y[50];

The population of the array is done in another class,

geoGraph.y[x] = nums[x];

The array should be checked for null in the following code;

    int x=0;
    for(int i=0; i<sizeof(y);i++){
        //check for null
        p[i].SetPoint(Recto.Height()-x,y[i]);
        if(i>0){
            dc.MoveTo(p[i-1]);
            dc.LineTo(p[i]);

        }
        x+=50;
    }
5
  • 2
    What is the type of the array? Unless it contains pointers, or types that can be implicitly constructed from NULL, you cannot really do this. Commented Oct 2, 2013 at 8:01
  • Show more code - how you initialize array, how are you filling it with data. Now it seems you are misinterpreting what is NULL - it's for not allocated pointers, but still - the pointer needs to be initialized to be NULL. Also this is more C than C++. Commented Oct 2, 2013 at 8:02
  • 2
    The requirement doesn't really make sense. You cannot have an object in C++ that "isn't there". Once you have an object, you have an object. Commented Oct 2, 2013 at 8:08
  • NULL is zero. Zero is normally a valid value for an integer. Unless you consider one value (such as zero) as "invalid" there's no way short of using an array of tuples (or, alternatively, two arrays) of getting what you want. One value in the tuple would be the integer to store, and the other would be a bool holding the "validity". Commented Oct 8, 2013 at 9:33
  • @Damon NULL and 0 are both null pointer literals, but they might not be the same type. Commented Apr 23, 2020 at 10:09

4 Answers 4

21

If your array is not initialized then it contains randoms values and cannot be checked !

To initialize your array with 0 values:

int array[5] = {0};

Then you can check if the value is 0:

array[4] == 0;

When you compare to NULL, it compares to 0 as the NULL is defined as integer value 0 or 0L.

If you have an array of pointers, better use the nullptr value to check:

char* array[5] = {nullptr}; // we defined an array of char*, initialized to nullptr

if (array[4] == nullptr)
    // do something
Sign up to request clarification or add additional context in comments.

5 Comments

It works for me: ideone.com/4n7vz2. I checked without C++11 enabled and it also works. What compiler are you using ?
@Madz Could you post the complete code. This is something that was defined as early as K&R C, and works with every compiler I've ever used.
The code's a bit too big, I use VS 2008. I used std::fill(y, y + 50, -1); as mentioned in stackoverflow.com/questions/2890598/…. Thanks for the help.
A question: what if the array does not contain ints? For strings, I could see something like '0', but what about specialized types not inbuilt into C++?
@DROPTABLEnames If you have some type T for which there is no notion of "empty", there simply isn't a notion of "an empty element of a T array"
4

You can use boost::optional (or std::optional since C++17), which was developed in particular for decision of your problem:

boost::optional<int> y[50];
....
geoGraph.y[x] = nums[x];
....
const size_t size_y = sizeof(y)/sizeof(y[0]); //!!!! correct size of y!!!!
for(int i=0; i<size_y;i++){
   if(y[i]) { //check for null
      p[i].SetPoint(Recto.Height()-x,*y[i]);
      ....
   }
}

P.S. Do not use C-type array -> use std::array or std::vector.

std::array<int, 50> y;   //not int y[50] !!!

3 Comments

Is std::array<int, 50> like the ArrayList in java?
@Madz en.cppreference.com/w/cpp/container/array If you are using C++, use the STL libarary. Especially STL containers.
@Madz std::vector<int> is more similar to Java's ArrayList<Integer>, because you can change the number of elements. std::array<int, 50> is always exactly 50 ints
3

If the array contains integers, the value cannot be NULL. NULL can be used if the array contains pointers.

SomeClass* myArray[2];
myArray[0] = new SomeClass();
myArray[1] = NULL;

if (myArray[0] != NULL) { // this will be executed }
if (myArray[1] != NULL) { // this will NOT be executed }

As http://en.cppreference.com/w/cpp/types/NULL states, NULL is a null pointer constant!

4 Comments

Actually, in C++ NULL must be an integer, such as 0 or 0L. This doesn't make it right for use with integers, though - it's supposed to represent a null pointer. With C++11 and nullptr, new code should steer clear of NULL.
@Angew A good compiler can still warn if you use NULL in a context where it isn't a pointer.
@Angew NULL "an integer literal with value zero, or a prvalue of type std::nullptr_t (since C++11)"
@Caleth Fair enough. I'd like to see a compiler author brave enough to define NULL to nullptr, though. There's mountains of code out there which use NULL in contexts where only integers can be used.
1

There is no bound checking in array in C programming. If you declare array as

int arr[50];

Then you can even write as

arr[51] = 10;

The compiler would not throw an error. Hope this answers your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.