I am writing my own library for using with my Arduino. In my code, if I set a pointer I have declared to NULL
, such as
int *ptr=NULL;
I get the error
error: ‘NULL’ was not declared in this scope
I need to initialize it to NULL
as I am using pointers to implement a list.
If I use NULL
directly in the program in the IDE, I do not get this error. This seems to be happening only when it is a part of the library code.
How do I correct this? Is there any alternative keyword to use? Or do I have to include some library?
Thank you.
2 Answers 2
I am writing my own library for using with my Arduino.
You should really include Arduino.h, i.e.
#include <Arduino.h>
Not only will that define NULL
for you, but you also get the other standard Arduino functions like digitalRead
, pin number declarations, and various useful macros.
-
Thank you. Is there any reason to prefer it over including individual libraries?GoodDeeds– GoodDeeds2017年02月10日 14:09:08 +00:00Commented Feb 10, 2017 at 14:09
-
1Including Arduino.h gives you all the stuff you should need (including the standard libraries). If you ever need something like a port or pin address, or any of the Arduino functions (like analogRead) you are going to have to include Arduino.h anyway.2017年02月10日 21:27:28 +00:00Commented Feb 10, 2017 at 21:27
You should make sure your header includes the stddef.h
header, which holds all these standard definitions (hence its name):
#include <stddef.h>
stddef.h
, but that gets included by default anyway, and is used by many other headers, likestdio.h
.NULL
in a definition of a header file which I have written. I hadn't realized that it works otherwise. Edited by question.