在哪里/如何定义null?

时间:2013-09-29 04:41:16

标签: c++ null

是否未声明Null?

我的代码:

// 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;
}

[错误]'null'未在此范围内声明

如何声明'null'?我试过包括其他几个库。 我正在使用Dev C ++ v5.4.2

谢谢,~d

2 个答案:

答案 0 :(得分:6)

它不是null。所有上限都是NULL。如果编写NULL不起作用,您可以使用

自行定义
#define NULL 0

答案 1 :(得分:3)

使用NULL代替Null。

如果您使用它来初始化指针而您使用的是C ++ 11,请使用nullptr

尽管NULL用于分配指针(即使NULL不是指针类型但是整数),但在下面的情况下可能会遇到问题:

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

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

有关详细信息,请参阅THIS