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?
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'
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
.
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 ??.
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