-5

in C you have many datatypes… and all these datatypes have a different zero initializer… examples

int i = 0;
float f = 0.0;
const char *str = NULL;
struct myStruct *myS = NULL;
long l = 0L;

etc, I now asking for a ZERO initializer to set everything to NULL or 0…. The example from above would look like…

int i = ZERO;
float f = ZERO;
const char *str = ZERO;
struct myStruct *myS = ZERO;
long l = ZERO;

this would be useful if you write a macros like…

#define highREAD(T,TT,TTT) { \                                                                                     
  TT val = ZERO; \                                                                                                 
  MqErrorCheck(MqRead##T(mqctx,&val)); \                                                                           
  LngErrorCheck(PyList_Append(RET, TTT)) \                                                                         
  break; \                                                                                                         
}                                                                                                                  

with a datatype as part of macro-parameter…

thanks.

jww
  • 97,681
  • 90
  • 411
  • 885
Andreas Otto
  • 311
  • 1
  • 10
  • 3
    Don't do this. Macros are useful, but don't get too fancy with them. It's a typical beginners' problem and I don't expect you follow this decades old advice. Write read-able and **debugable** code instead. Siad that: what is your **specific** question? – too honest for this site Dec 09 '18 at 10:57

1 Answers1

3

This ZERO can be simply 0.

int i = 0;                // No explanation needed
float f = 0;              // Floating point conversion, exact and well defined.
const char *str = 0;      // 0 is a null pointer constant (1)
struct myStruct *myS = 0; // Same as above
long l = 0;               // Integral conversion

Live example


(1) - An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. [n1570]

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Nevertheless it's better to be clear about the values and use the expected type even for zero. For static variables, not using an explicit initialiser at all might be a good alternative, though. – too honest for this site Dec 09 '18 at 10:58
  • Just a note: NULL does not have the have the bit-pattern of 0. While `void *p = 0` is equivalent to `void *p = NULL`, it is not the same as using `memset(&p, 0, sizeof(void *))`. The compiler just sees the 0 in `void *p = 0` as indicating a NULL. See this question: https://stackoverflow.com/questions/9894013/is-null-always-zero-in-c – Byte Dec 11 '18 at 02:45