0

I am learning C, used to code in Ruby. So how do I return NULL from a integer result type function? for example:

int valueOf(t_node *node, int n){
    int current = 0;
    int value;
    while (node -> next != NULL) {
        value = node -> val;
        if (current == n) {
            return value;
        }
        if (current > n) {
            return NULL;
        }
        node = node -> next;
        current += 1;
    }
}

I wanted function to return NULL if current > n is true.

Nodir Nasirov
  • 1,488
  • 3
  • 26
  • 44

2 Answers2

4

In C, NULL is just a synonym for 0. So you really only need to do this...

if (current > n) {
    return 0;
}

However, NULL usually refers to a pointer value that is undefined and not an integer. In C, integer values are not references as they are in many interpreted languages. They are scalar and can't be referred to implicitly with a pointer.

If you want to indicate an error condition or undefined behavior when current > n, you will have to provide a separate mechanism for indicating that the value isn't usable. Usually, C functions will return a -1 on an error. Since you are using the integer return for a value, that would mean that a valid value could never be -1.

It looks like you're handling a linked list and you want to limit the number of items to be checked. A possible way around this might be...

int valueOf(t_node *node, int n, int *val){
    int current = 0;
    int value;
    while (node -> next != NULL) {
        value = node -> val;
        if (current == n) {
            // This notation is for dereferencing a pointer.
            *val = value;
            return 0;
        }
        if (current > n) {
            return -1;
        }
        node = node -> next;
        current += 1;
    }
    // This method also gives you a way to indicate that
    // you came to the end of the list. Your code snippet
    // would have returned an undefined value if node->next == null
    return -1;
}
SteveB
  • 483
  • 1
  • 4
  • 18
4

[H]ow do I return NULL from a integer result type function?

You don't. NULL is a macro representing a value of type void *. It is not an int, so a function returning int cannot return NULL.

Now, it is possible to convert NULL or any other pointer to type int, but the result of such a conversion is a valid, ordinary int. You seem to be looking instead for some kind of distinguished value, but there is none available unless you reserve such a value yourself. For example, you might reserve INT_MIN for that purpose. Of the built-in types, only pointer types afford general-purpose distinguished values (null pointers).

To provide for your function to signal failure to the caller, you have a couple of alternatives to reserving a value. The most common one is to use the function's return value only to report on the success or failure of the call, and to deliver any output -- in your case a node's value -- via a pointer argument:

int valueOf(t_node *node, int n, int *result) {
    int current = 0;

    while (current < n && node != NULL) {
        node = node->next;
        current += 1;
    }

    if (node == NULL) {
        // no such node -- return a failure code
        return 0;
    } else {
        // current == n
        *result = node->value;
        return 1;
    }
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157