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

戏子无情 提交于 2019-12-20 06:36:45

问题


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 return NULL because is is expecting an int in the return statement.

But the function struct node* find(int key), when working with linked lists allows me to return NULL.


回答1:


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....




回答2:


It is a matter of convention and you should clearly have one in your head and document it (at least in comments).

Sometimes a pointer really should always point to a valid address (see this intSwap example, both arguments should be valid pointers). At other times, it should either be such a valid address, or be NULL. Conceptually the pointer type is then by convention a sum type (between genuine pointer addresses and the special NULL value).

Notice that the C language does not have a type (or a notation) which enforces that some given pointer is always valid and non-null. BTW, with GCC specifically, you can annotate a function with __attribute__ using nonnull to express that a given argument is never null.

A typical example is FILE* pointers in <stdio.h>. The fopen function is documented to be able to return NULL (on failure), or some valid pointer. But the fprintf function is expecting a valid pointer (and passing NULL to it as the first argument is some undefined behavior, often a segmentation fault; and UB is really bad).

Some non-portable programs even use several "special" pointer values (which should not be dereferenced), e.g. (on Linux/x86-64) #define SPECIAL_SLOT (void*)((intptr_t)-1) (which we know that on Linux it is never a valid address). Then we could have the convention that a pointer is a valid pointer to a valid memory zone, or NULL or SPECIAL_SLOT (hence, if seen as an abstract data type, it is a sum type of two distinct invalid pointers NULL and SPECIAL_SLOT and the set of valid addresses). Another example is MAP_FAILURE as result of mmap(2) on Linux.

BTW, when using pointers in C to heap allocated data (indirectly obtained with malloc), you also need conventions about who is in charge of releasing the data (by using free, often thru a supplied function to free a data and all its internal stuff).

Good C programming requires many explicit conventions regarding pointers, and it is essential to understand them precisely and document them well. Look for example[s] into GTK. Read also about restrict.




回答3:


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

In general, if and only if the function returns a pointer type:

T * function(<parameter definitions> | void>); /* With T being any valid type. */

There are other, corner cases, which depend on the C implementation in use.




回答4:


NULL is definitely a pointer. So if your function is expected to return a pointer and for some reason cannot, it should return the obvious "invalid pointer", which is NULL.




回答5:


NULL may be defined as 0 or (void*)0 (See 6.3.2.3p3 and 7.19p3).

Consequently, it may be always used as a return value in functions returning a pointer type, and depending on implementation, it may be potentially usable as a return value in functions returning numerical types, though the latter is a bad idea, as NULL is expected to be used in association with pointers.



来源:https://stackoverflow.com/questions/40946981/when-are-you-able-to-return-null-as-the-returning-value-of-a-c-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!