Passing NULL to puts () and other functions: how to suppress the "null argument where non-null required" warning with an analogue if (NULL) puts (NULL);

Question:

There is a macro like this:

#define __ex_msg (__ex_idx && __ex_idx < TCM_MAX) ? \
                 __ex_msgs[__ex_idx-1] : (const char *)0

Accordingly, in this design:

if(__ex_msg) puts(__ex_msg);

We get a fair splash:

null argument where non-null required (argument 1) [-Wnonnull]

The only thing that comes to mind so far is to break it into 2 macros, something like this:

#define __ex_have_msg (__ex_idx && __ex_idx < TCM_MAX) ? \
                      __ex_msgs[__ex_idx-1] : (const char *)0
#define __ex_msg __ex_msgs[__ex_idx-1]

But for some reason I don't really like this. What else can you think of here, besides __ex_msg() into a function? Do not offer to disable warnings 🙂

Answer:

puts() required

if(__ex_msg){
  fprintf(stderr,"%s\n",__ex_msg)

or you can transform define

#define __ex_msg  if( __ex_idx && __ex_idx < TCM_MAX) \
                    puts(__ex_msgs[__ex_idx-1]); 
Scroll to Top