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

I don't understand what this line does,anyone knows?

compile-fan
  • 16,885
  • 22
  • 59
  • 73

6 Answers6

4

That's a rather odd way of writing a test for the return of a null pointer which indicates an error in fgets().

I'd write it like this:

if (!fgets(line, sizeof(line), stdin))
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 4
    That's personal taste. My personal taste would always to be compare with `NULL`, because that follows the semantics of the function (it's not returning a boolean). – Oliver Charlesworth May 20 '11 at 14:01
  • I agree with Oli, and this opinion is also shared by the widely recognized MISRA-C standard. Only boolean expressions should be compared with boolean operators. – Lundin May 20 '11 at 14:10
  • @Oli @Lundin My feeling is that you can't make a silk purse from a sow's ear, and that there's little point trying. – David Heffernan May 20 '11 at 14:45
3
(char*) 0

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

A character containing the value 0 would be:

(char) 0
Hyperboreus
  • 822
  • 5
  • 4
  • 1
    It's not a pointer to a character @ address 0, it's a pointer to null character (http://www.lysator.liu.se/c/c-faq/c-1.html) – KevinDTimm May 20 '11 at 14:07
  • @KevinDTimm char* c = (char*) 0 points to 0 whatever value may be there (although it doesn't matter as on most platform it would be illegal memory). – Hyperboreus May 20 '11 at 14:10
  • @compile-fan the type is different. The literal 0 should be on most implementation of type int (being int and n-bit integer as wide as your registers). (char*) simply casts it to a char-pointer. – Hyperboreus May 20 '11 at 14:11
  • What does `(char *)1` mean then? – compile-fan May 20 '11 at 14:19
2

It means a null pointer to char. It would be the same if you replace the (char*)0 with NULL. In this particular case it is checking if there is nothing more to read from the stdin. I think is just a way to be cryptic and showing some spectacular and beautiful features. If you replace it with a NULL you gain in readability without changing the semantic.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • actually, it means a pointer to the null char - there is a difference – KevinDTimm May 20 '11 at 14:05
  • 1
    @Kevin Actually, it doesn't. fgets() returns a pointer that may be NULL. So this code is just a weirdo way of comparing the result against NULL, it has nothing to do do with the nul character. – Lundin May 20 '11 at 14:13
  • ignoring fgets(), IMHO (char *)0 means - pointer to null char, or char pointer to null, but not null pointer to char - it seems to be just semantics, but I believe there is a difference. – KevinDTimm May 20 '11 at 14:18
  • It doesn't mean a pointer to the "null char", which is (char)0. In this case the pointer is zero, while to be a pointer to the null char, the pointer would have to be non-null and point to a memory address where the first byte is set to 0. – Jamie May 20 '11 at 16:46
1

The line checks if fgets return 0. The cast to char* is only to match the return type of fgets:

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

But 0 is implicit converted to char* if you remove it.

If you need more information on fgets look here

Fox32
  • 13,126
  • 9
  • 50
  • 71
1

(char*)0 creates the "null" pointer. So you are seeing if the value of fgets is null.

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

The full statement appears to be checking if you are at the end of the file, and if so breaking out (though that's a guess).

Jamie
  • 3,901
  • 3
  • 23
  • 27
-1

It is simply checking the results of fgets for null character pointer.

According to cplusplus.com

Return Value

On success, the function returns the same str parameter. If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned.

If an error occurs, a null pointer is returned.

Use either ferror or feof to check whether an error happened or the End-of-File was reached.

Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135