0

Null is not declared?

My code:

// Include necessary libraries
#include <cstdlib> // Exits
#include <iostream> // I/O
#include <cstring> // String functions
using namespace std;

int main(int argc, char *argv[])
{
    //Declare local Constants and Variables
    const char SOKINP[19] = "23456789TtJjQqKkAa"; // Valid Input Characters
    char acCards [5]; // Array to hold up to five cards (user input)
    bool bErr;        // Loop on Error (Calculated)
    int  i,           // Loop variable (Calculated)
    iNbrCrd,          // Number of Cards 2-5 (user input)
    iNAces,           // Number of Aces (Calculated)
    iTS;              // Total Score (Calculated)

    ...

    for (i=0;i<iNbrCrd;i++){
       do {
           cout << "Enter Card #" << i << " (2-9,t,j,q,k or a)  >";
           cin  >> acCards[i];
           cout << endl;
           bErr = (strchr(SOKINP, acCards[i]) == null) ? true : false; // *ERROR*
       } while (bErr);
    }
    return EXIT_SUCCESS;
}

[Error] 'null' was not declared in this scope

How do I declare 'null'? I tried including several other libraries. I'm using Dev C++ v5.4.2

Thanks, ~d

Dave C
  • 37
  • 2
  • 7
  • 5
    Use `NULL` or `nullptr`. – ta.speot.is Sep 29 '13 at 04:41
  • 1
    @ta.speot.is: You should say : Use `nullptr` (or `NULL`). The order emphasizes and the emphasise matters. – Nawaz Sep 29 '13 at 04:46
  • @Nawaz He's using Dev C++. Unless it's been updated, `nullptr` is not going to work for him. – ta.speot.is Sep 29 '13 at 04:46
  • 4
    @ta.speot.is In this case, you should say: "don't use Dev-C++ but a good C++ compiler, and use `nullptr`". –  Sep 29 '13 at 04:51
  • 0 works just fine and should work for all compilers. – Carey Gregory Sep 29 '13 at 04:56
  • @CareyGregory: yes that should work for all compilers, though **not** in all situations which is why `nullptr` was added to the language. – Nawaz Sep 29 '13 at 04:58
  • @Nawaz - In what situation does `0` not work? – Carey Gregory Sep 29 '13 at 04:59
  • 1
    @CareyGregory: [this topic](http://stackoverflow.com/questions/13816385/what-are-the-advantages-of-using-nullptr) gives lots of such example. See every answer. – Nawaz Sep 29 '13 at 05:03
  • @Nawaz - Meanwhile, before I've even had a chance to read your link, a C solution to the problem has garnered 5 upvotes and been accepted. So I'll go read the link now and maybe learn something instead of hanging around this thread. :-) – Carey Gregory Sep 29 '13 at 05:06
  • 1
    @Nawaz +1. The overloading example is compelling. – Carey Gregory Sep 29 '13 at 05:10
  • @H2CO3 - Perhaps it's from being a noob, but I think Dev-C++ is a good compiler (at least from an 'ease of use' standpoint.) What compiler do you recommend (for Win 7 Ult, x64)? – Dave C Sep 30 '13 at 02:25
  • I had some char to short conversion error I couldn't resolve (later in my code) and ended up using the .find method with size_t type and string::npos as equality verifier. – Dave C Sep 30 '13 at 02:38
  • @DaveC On Windows, either use 1. Microsoft's Visual Studio IDE that includes their own compiler, or 2. the Code::Blocks IDE and/or the MinGW environment, which both come with GCC. –  Sep 30 '13 at 04:17

2 Answers2

6

Its not null. It's NULL in all caps. If writing NULL does not work, you can define it yourself by using

#define NULL 0
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
The Light Spark
  • 504
  • 4
  • 19
  • 6
    @ValekHalfHeart You will get type mismatch warnings **exactly** when you define it as `(void *)0`. In C and C++, `0` is a valid literal for null pointers of **any type**, but `(void *)0` only works with pointers to `void`, due to strict strong typing in C++. Thus, `(void *)0` is safer in C than `0`, but it's **wrong** in C++. –  Sep 29 '13 at 04:52
  • Noob mistake. Doh! Thank you everyone, capitalizing it was the answer. – Dave C Sep 29 '13 at 04:59
3

Use NULL instead of Null.

If you are using it to initialize a pointer and you are using C++11, use nullptr.

Although NULL works for assigning the pointers(even though NULL is not a pointer type but is integer), you may face problems in the below case:

void func(int n);
void func(char *s);

func( NULL ); // guess which function gets called?

Refer to THIS for more details

Saksham
  • 9,037
  • 7
  • 45
  • 73