## imolorhe -6

What is the integer or double equivalent of the NULL character. That is, the integer that can be entered to represent NULL when using integers (supposing 0 is accepted as a valid integer and not a NULL).

## jonsca 1,059

It might be helpful if you gave a brief snippet of what you are trying to do.

## imolorhe -6

I want to make an array of numbers that is filled with the NULL value. The computer checks the particular index in the array to know if it contains a value or not.

## jonsca 1,059

To my knowledge, you won't be able to do that. Two things I could think of would be, depending on the range of your valid numbers, use a sentinel value (so if your data are guaranteed to go from -100 to 100 use -999 or 999). Another possibility would be to have a parallel array of boolean values. If there's a value in a slot of the numeric array, set the slot in the boolean array corresponding to it to true.

Again, someone may have a solution that's simpler or more effective, so stay tuned.

## WaltP 2,905

NULL is 0. There is no distinction between a variable with no value vs. 0. As jonsca said define a value as your "no value" number.

## Banfa 597

Or a set would work, a set of the actual values would returned not found if you looked up a non-existing value.

On NULL it really only applies to pointers. It is a special representation for the platform that indicates that the pointer is invalid. The NULL pointer for a platform does not have to have the value 0 but a compliant C++ compiler does have to convert the value 0 used in a pointer context in the code to the platforms NULL pointer value. Of course on many(most?) platforms the NULL pointer value for the platform is actually 0 so the conversion is quite simple.

This should not be confused with NUL the name of one of the control characters (the first 26 characters) in the ASCII character set that has the value 0 or '\0'.

Neither NULL or NUL apply to any form of integer(int) or floating point type.

commented: great answer +35
commented: Good one. +6

## vijayan121 1,152

What is the integer or double equivalent of the NULL...The computer checks the particular index in the array to know if it contains a value or not.

What we are talking about here is the null in the relational database world; the SQL NULL, which essentially says that "this information is missing" or "no information has been stored for this".

For example, "What was the int that the user entered as input?"
78 - the user entered 78
-3 - the user entered -3
0 - the user entered 0
null - we do not know. This information was never entered, or if it was entered, it was not stored anywhere.

The double (floating point) equivalent of the SQL NULL could probably be a NaN http://en.wikipedia.org/wiki/NaN

For int, there is no equivalent "not an int". We would have to create a user-defined type to represent that. For example:

``````#include <stdexcept>

struct int_or_null
{
int_or_null() : nothing(true) {}
int_or_null( int i ) : something(i), nothing(false) {}

bool is_null() const { return nothing ; }
operator bool() const { return !nothing && something ; }

int_or_null& operator= ( int i ) { something = i ; nothing = false ; return *this ; }
int_or_null& make_null() { nothing = true ; return *this ; }

// comparison operators for null are similar to those for NaN
bool operator== ( const int_or_null& that ) const
{ return nothing ? false : !that.nothing && something==that.something ; }
// other comparison operators

operator int& () { if(nothing) throw std::domain_error("null") ; return something ; }
operator const int& () const { if(nothing) throw std::domain_error("null") ; return something ; }

// ...

int something ;
private: bool nothing ;
};``````

You may want to consider using tri-state boolean logic instead. http://en.wikipedia.org/wiki/Null_%28SQL%29#Three-valued_logic_.283VL.29
Perhaps use boost::logic::tribool http://www.boost.org/doc/libs/1_45_0/doc/html/tribool.html

commented: my first thought too :) +35

## Red Goose 21

Is there any particular reason you can't use a vector? Because it seems that iterators would do all that for you.

Or a list, if your planning to constantly access different points.

I'm sure someone could figure out a way to do it, but it would ultimately feel needless since something that does exactly that already exists.

## firstPerson 761

Can you settle for something like this :

``````#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
using namespace std;

template<typename T>
class Table{
private:
class TWrapper{
private:
T value_;
bool isNull_;
public:
TWrapper(const T& val = T(), bool isnull = true) : value_(val), isNull_(isnull){}
bool isNull(){ return isNull_;}
void isNull(bool b){ isNull_ = b; }
T& get(){ return value_;}
};
private:
typedef TWrapper Element;
typedef std::vector<Element> TableType;
TableType table;
public:
Table(){};
Table(int size){ _init(size);}
void setAt(int index, const T& value){
table[index] = Element(value,false);
}
T& getAt(int index){ return table[index].get(); }
bool hasElementAt(int index){
return _isValidIndex(index) && !table[index].isNull();
}
int size(){
return table.size();
}
private:
bool _isValidIndex(int i){
return (i >= 0 && i < (int)table.size() );
}
void _init(int size){
table = TableType(size,Element()); //{null....null}
}
};
int main(){
Table<int> table = Table<int>(3);  //can assume table contains {null null null}
cout << boolalpha;
for(int i = 0; i < table.size(); ++i){
if(table.hasElementAt(i)) cout << table.getAt(i) << endl;
else cout << "No element at index : " << i << endl;
}

table.setAt(0,150);
table.setAt(2,100);

for(int i = 0; i < table.size(); ++i){
if(table.hasElementAt(i)) cout << "\nAt index " << i << " = " << table.getAt(i);
else cout << "\nNo element at index : " << i;
}
cout << endl;
}``````

basic idea is to wrap the type T. Its less efficient, but possible does what you want.