0

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.

asked Feb 8, 2017 at 14:35
2
  • 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. Commented 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. Commented Feb 8, 2017 at 14:46

2 Answers 2

3

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.

answered Feb 8, 2017 at 21:31
2
  • Thank you. Is there any reason to prefer it over including individual libraries? Commented 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. Commented Feb 10, 2017 at 21:27
2

You should make sure your header includes the stddef.h header, which holds all these standard definitions (hence its name):

#include <stddef.h>
answered Feb 8, 2017 at 14:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.