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).

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.

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.

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.

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.

Comments
great answer
Good one.

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

Edited 5 Years Ago by vijayan121: n/a

Comments
my first thought too :)

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.

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.

This article has been dead for over six months. Start a new discussion instead.