0

Would this be a valid use of NULL in C or are there other ways to solve this problem that are preferred?

// Send data
// cb_push returns NULL if it is successful
char uart_send(char c) {
    void* ret = cb_push(w_buffer, &c);
    if (ret != NULL) return c;

    SETBIT(UCSR0B, UDRIE0);
    return NULL;
}

In Java I would do like this, sort of, but in C I don't know what is good practice.

evading
  • 3,032
  • 6
  • 37
  • 57
  • 1
    `NULL` is usually just 0, so you're technically probably returning a null character (which is different than from a pointer perspective). – chris Oct 29 '12 at 21:50
  • @chris or if it's `((void *)0`, then you'll get a diagnostic. –  Oct 29 '12 at 21:52
  • 1
    if `NULL` is `(void *) 0` which is frequent, the `return` statement would be invalid. – ouah Oct 29 '12 at 21:52
  • Those first two lines look odd to me. Are you saying that `cb_push` returns a NULL pointer *upon success*? It probably just returns 0 (unless you wrote it) – Ed S. Oct 29 '12 at 22:02

3 Answers3

2

It's not really defined and there are different approaches depending on the library and/or function you're using. In general, there's no way to differentiate between 0 and NULL (in fact, NULL is usually just a preprocessor macro expanding to 0).

In general, the following possibilities are used, sometimes even matched within one library depending on the usage:

  • If a pointer is returnd, a return value of 0 usually indicates some kind of error.
  • Functions with status codes (or main entry points) usually return 0 in case there hasn't been any error.
  • There are functions returning 0 if something hasn't been successfull (i.e. they return a boolean value).
  • Some stdlib string functions return an "absurd" value in case there has been an error (or nothing found). For example, std::string::find() will return -1 if the sub string couldn't be found. This is however wrapped/hidden behind a constant named value (std::string::npos) to avoid throwing around "magic values".

Is there a perfect way? I don't think so, but it really depends on the specific use case. If you return a pointer, returning 0 in case of a mistake is just perfect. If you're returning status codes, I'd go with either macros (similar to windows API) or enums. Don't even worry about any specific values - only use the names.

Mario
  • 35,726
  • 5
  • 62
  • 78
0

Generally speaking, the C convention is to return 0 on success or a negative number if not.

Also, assuming your function is supposed to return some pointer and it failed to do so, the convention is to return NULL.

Fingolfin
  • 5,363
  • 6
  • 45
  • 66
-1

NULL is internally defined as #define NULL (void*)0 means a pointer pointing to zeroth location of memory.

  • Please see [Is it possible to access physical address 0?](https://stackoverflow.com/q/26735775/1233251). The null pointer is not to be interpreted as pointing to any location in memory. – E_net4 Sep 06 '19 at 13:02
  • The value of `NULL` is not specified in the C standard in [**7.19 Common definitions **](https://port70.net/~nsz/c/c11/n1570.html#7.19p3): "The macros are `NULL` which expands to an implementation-defined null pointer constant...". The reason for [**6.3.2.3 Pointers**, paragraph 3](https://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p3) is the widespread and incorrect assumption that `NULL` is numeric zero. The following is nothing but a sop to bad code: "An integer constant expression with the value 0, or such an expression cast to type `void *`, is called a null pointer constant." – Andrew Henle Sep 06 '19 at 13:06
  • please look for the definition of NULL which is defined in stdio.h file, the definition given in that is #define NULL ((void *)0) – akshay chaudhari Feb 07 '20 at 05:51
  • @E_net4theharmedSOmember we are not accessing the zeroth location, accessing means dereferencing the pointer, and hence if we dereference any pointer which is NULL then we get segmentation fault, because that memory is not accessible to our program, please have a look at the definition of NULL in stdio.h. – akshay chaudhari Feb 07 '20 at 05:56
  • @akshaychaudari Even though the null pointer constant is built by casting an expression with the value 0, that does not make it a representation of the zeroth location in memory. My point stands, and the other answers appear to explain the subject better. – E_net4 Feb 07 '20 at 08:28