When are you able to return NULL as the returning value of a C function?

后端 未结 5 1678
不思量自难忘°
不思量自难忘° 2021-01-26 07:22

I was wondering if you could tell me when you are able to return NULL, as the result of a function in C.

For instance int lenght() can\'t retur

5条回答
  •  清酒与你
    2021-01-26 07:32

    NULL is a pointer value - or rather a null-pointer value.

    NULL means that the function can't find where your pointer should point to - for example if you want to open a file, but it doesn't work your file pointer is returned as NULL. So you can test the value of a pointer and check to see if it worked or not.

    If you are writing a routine

    int length()
    

    then you could return a negative value if length is unable to read the length of whatever you send it - this would be a way of indicating an error, because normally lengths can never be negative....

提交回复
热议问题