-
int NULL
Is there anyway to int = NULL; perform more like ch = NULL;?
I am supposed to "delete" entries in a file that contains struct values (one int and one char), but when you NULL the int part of the struct and print the new value it shows 0, not nothing in it's place. (which is what I need to do)
-
Code:
if (myint == 0) /* nothing */;
else printf("%d", myint);
Voila!
-
Well I am overwriting the data in a file with new information, which I mean technically it would work to the viewer of the file (though would assume the information is gone even though the data is still there, just overlooked)
-
Not sure I entirely understand, but an int can not contain "nothing". NULL actually is basically the same thing as 0. The difference is that when you assign a null character to the beginning of a character array, functions that treat the character array as a string will only work with data before the null character - in that case "nothing". But what you're really doing behind the scenes is assining a byte with the value '0' to the first element of the array.
-
Yea that is what I figured, I was just wondering if there was a roundabout way to do it but I suppose there isn't! Thank's though!
-
NULL should not be assigned to an int (or any other integer type); NULL is strictly for pointers. It is not for characters, either (which are really just integers).
If you have an int value that can be “invalid”, you can pick a value that the int will never take on (e.g. -1 if the values are all positive) and check for -1 before printing.
You can also use a companion boolean-style object that keeps track of whether the value is valid.
Examples:
Code:
/* first example */
int val = get_value();
if(invalid) val = -1;
if(val != -1) printf("%d\n", val);
/* second example */
int val = get_value();
int has_value = 1;
if(invalid) has_value = 0;
if(has_value) printf("%d\n", val);
-
NULL vs. NUL
"Two L's points to no thing
One L is the end of an ASCII string"
Looking at the definition of the pointer NULL in my version of CodeBlocks
Code:
#define NULL ((void *)0)
You should use '\0' for NUL, because the ASCII value of NUL is not defined by default.
http://faq.cprogramming.com/cgi-bin/...&id=1043284376