What does (char *) 0 mean in c?

if ( fgets( line, sizeof(line), stdin ) == (char*) 0 )... 

I do not understand what this line does, does anyone know?

+4
source share
6 answers
 (char*) 0 

It is not a null character, but a pointer to a character at address 0.

The character containing the value 0 will be:

 (char) 0 
+3
source

This is a rather strange way to write a test to return a null pointer that indicates an error in fgets() .

I would write it like this:

 if (!fgets(line, sizeof(line), stdin)) 
+4
source

This means a null pointer to char. It would be the same if you replace (char *) 0 with NULL. In this particular case, it checks to see if there is anything else to read from stdin. I think this is just a way to be mysterious and show some impressive and beautiful features. If you replace it with NULL, you will get readability without changing semantics.

+2
source

The string checks to see if fgets 0 is returned. Casting to char* matches only the return type of fgets :

 char * fgets ( char * str, int num, FILE * stream ); 

But 0 is implicitly converted to char* if you delete it.

If you need more information on fgets , check here.

+1
source

(char *) 0 creates a null pointer. So you see that the value of fgets is null.

The documentation for fgets states that it returns null when there is an error, or you have reached the end of the file.

The full statement seems to check if you are at the end of the file, and if that happens (although this is an assumption).

+1
source

This is just checking the fgets results for a null character pointer.

According to cplusplus.com

Return value

If successful, the function returns the same str parameter. If the end of the file is encountered and no characters have been read, the contents of str remain unchanged and the null pointer is returned.

If an error occurs, a null pointer is returned.

Use either ferror or feof to check if an error has occurred or if the end of the file has been reached.

-1
source

All Articles