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.

  • 1
    Works fine for me. No need to include anything. It is, actually, part of stddef.h, but that gets included by default anyway, and is used by many other headers, like stdio.h.
    – Majenko
    Feb 8, 2017 at 14:40
  • @Majenko Thank you. I checked, it works when I use directly. I forgot to mention that this error appears when I use NULL in a definition of a header file which I have written. I hadn't realized that it works otherwise. Edited by question.
    – GoodDeeds
    Feb 8, 2017 at 14:46

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
    Feb 10, 2017 at 14:09
  • 1
    Including 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.
    – Nick Gammon
    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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.