NUL undeclared- first use in this function

不想你离开。 提交于 2019-12-18 09:12:46

问题


From the web I understand that C contains NUL keyword.

But while compiling I get an error

NUL undeclared first use in this function'

My code fragment:

for (;;) {
        char ch = (*pzDest = *pzSrc);
        if (ch == NUL)
            break;
        if (ch == ':') {
            *pzDest = NUL;
            break;
        }
        pzDest++;
        pzSrc++;
    }

Any idea why I get this error?


回答1:


There's NULL and then there's NUL.

NULL is defined in stddef.h, is used very widely, and is a reference to a null pointer.

NUL is different - it is the first character in the standard ASCII character set, and more importantly, it is not a standard macro. You may have to define it yourself.

To define NUL, do:

#define NUL '\0'



回答2:


No, that's not standard. Add this at the beginning of your code or just use 0:

#define NUL 0

I infered you're not looking for NULL since you're doing ch == NUL

In various texts it is quite frequent to refer to '\0' as NUL.




回答3:


It's NULL, not NUL.

EDIT

Did you even read the link you posted? It says very specifically "NULL is not NUL. NUL is not a defined keyword in C++"

NULL is a macro defined in for the null pointer.
NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There?s no
standard macro NUL in C, but some people like to define it.
The digit 0 corresponds to a value of 80, decimal. Don?t confuse the digit 0 with the value of ?? (NUL)!
NULL can be defined as ((void*)0), NUL as ??.




回答4:


Defining your own NULL value is not standard, and very error prone and that will for sure give you no sleep nights and headaches in the best case.

Issues with the concept of NULL values between C and C++ and within C++ C++98, C++0x, and C++11 is not new, this is why from C++0x onward the nullptr macro has been introduced to avoid issues and harmonise things a bit.

If however you used code that inherits from issue of the legacy NULL burden:

Depending on your configuration, in the order in which your externals and libs are included etc, you might need to ensure that NULL is defined on time.

The easiest way is to include "stddef.h", note that the content of stddef.h may vary a lot depending on your system/linux flavor, this is an example:

#ifndef _LINUX_STDDEF_H
#define _LINUX_STDDEF_H

#undef NULL
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif

#endif

Example:

I had the issue trying to use the i2c library to drive i2c buses on a Raspberry-pi. In my case including the i2c lib wasn't enough, I had to create a utils.h file that I include in place of the i2c lib header:

#ifndef UTILS_H
#define UTILS_H

/// if you have to use i2c-dev.h then use this utils header instead of make sure to include stddef.h before.
/// otherwise you will end up with the following error:
/// .../arm-unknown-linux-gnueabi/arm-unknown-linux-     gnueabi/sysroot/usr/include/linux/i2c-dev.h:175: error: 'NULL' undeclared (first         use in this function)
/// return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
///                                                       ^
#include <stddef.h>
#include <linux/i2c-dev.h>
#endif // UTILS_H


来源:https://stackoverflow.com/questions/10546625/nul-undeclared-first-use-in-this-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!