Question:
NULL and 0?
anonymous
2007-11-13 02:28:14 UTC
i am using a function with variable arguments all of type integer:
#include
#include
int mul(int d,...)
{
va_list vList;
va_start(vList,d);
int num;
num = va_arg(vList,int);
while(num != NULL)
{
printf("\n[%d]",num);
num = va_arg(vList,int);
}
va_end(vList);
}
int main()
{
mul(1,2,3,4,5,NULL);
}
To identify the ending of list, i check the argument against NULL - (while loop)
but the problem is if i give 0 as the first argument, it is terminating prematurely.
why is it that 0 and NULL behave similarly?
what is the difference b/w the two?Exemplify if you can.
Five answers:
anonymous
2007-11-13 03:11:45 UTC
Question is why are you comparing NULL to an integer?

NULL should only be used in pointers assignment and comparison checks.

By definition, NULL is defined as a macro.



Under Borland Turbo C++.

NULL is defined as:

#if (defined(_Windows) || defined(__cplusplus))

#define NULL 0 // if compiling under Windows or C++.

#else

#define NULL ((void *) 0) // otherwise, make it a pointer.

#endif



If you want a terminating value, use a value which is unlikely to happen, such as largest negative integer or positive integer.
Pfo
2007-11-13 11:58:44 UTC
0 and NULL are the same thing in C, look where NULL is defined, it is 0. Consider that whenever you compile, all instances of the text NULL not contained within a string literal are replaced with 0.
cruppstahl
2007-11-13 04:38:54 UTC
i never use NULL, i always use 0. It's the same. A NULL pointer is a pointer which points to the address zero (0).
nirvana r
2007-11-13 02:41:59 UTC
In C/C++, NULL and 0 is the same.....you can use either the one.



The difference is NULL is a macro.



*********************************

you can join at www.dreamincode.net for free, read the forum rules then post a programming questions.
Erné R
2007-11-13 02:38:33 UTC
NULL = 0 and 0 = NULL... same thing


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...