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;
    }
Madz
  • 1,273
  • 2
  • 18
  • 35
  • 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. – juanchopanza Oct 02 '13 at 08: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++. – zoska Oct 02 '13 at 08: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. – Kerrek SB Oct 02 '13 at 08: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". – Damon Oct 08 '13 at 09:33
  • @Damon `NULL` and `0` are both null pointer literals, but they might not be the same type. – Caleth Apr 23 '20 at 10:09

4 Answers4

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
Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • 1
    It works for me: http://ideone.com/4n7vz2. I checked without C++11 enabled and it also works. What compiler are you using ? – Jaffa Oct 02 '13 at 08:36
  • @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. – James Kanze Oct 02 '13 at 08:44
  • The code's a bit too big, I use VS 2008. I used std::fill(y, y + 50, -1); as mentioned in http://stackoverflow.com/questions/2890598/how-to-initialize-all-elements-in-an-array-to-the-same-number-in-c. Thanks for the help. – Madz Oct 02 '13 at 09:01
  • 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++? –  Apr 20 '19 at 19:05
  • @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" – Caleth Apr 23 '20 at 10:17
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] !!!
SergV
  • 1,269
  • 8
  • 20
  • Is std::array like the ArrayList in java? – Madz Oct 02 '13 at 09:05
  • @Madz http://en.cppreference.com/w/cpp/container/array If you are using C++, use the STL libarary. Especially STL containers. – SergV Oct 02 '13 at 09:34
  • @Madz `std::vector` is more similar to Java's `ArrayList`, because you can change the number of elements. `std::array` is always exactly 50 `int`s – Caleth Apr 23 '20 at 10:12
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!

Victor Sand
  • 2,270
  • 1
  • 14
  • 32
  • 3
    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 is no longer proud of SO Oct 02 '13 at 08:03
  • 1
    @Angew A good compiler can still warn if you use `NULL` in a context where it isn't a pointer. – James Kanze Oct 02 '13 at 08:42
  • @Angew [NULL](https://en.cppreference.com/w/cpp/types/NULL) "an integer literal with value zero, or a prvalue of type `std::nullptr_t` (since C++11)" – Caleth Apr 23 '20 at 10:13
  • @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. – Angew is no longer proud of SO Apr 23 '20 at 11:14
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.

vkulkarni
  • 849
  • 1
  • 8
  • 12