Home > Back-end >  How do I return the first non-null value in c?
How do I return the first non-null value in c?

Time:10-18

In JavaScript, an expression like this 0 || "Hello World" returns "Hello World".

But in C, however, it is 1. The OR operator returns an int, instead of the actual value. I need the value to be returned instead.

How can I get the value instead of a boolean ?

I don't wanna write if else stuff with some scary declarations while dealing with logical snakes like this foo() || bar() || tar() || mar() || far(). Well if that is the only solution then I'm gonna jump back to VBA or VimScript to rewrite the compiler from scratch so it supports that feature. Or just gonna write binary values directly to the CPU, I don't care.


First check out the `code` below, please, and try to understand what it does.

I tried in this code below, but getting an error

test.c:16:16: warning: return makes pointer from integer without a cast [-Wint-conversion]

Here is where the error occurs, because the functino expect a pointer but the OR operator returns an integer.

return foo() || bee();

code

#include <stdio.h>
#include <stdlib.h>

char* foo()
{
  return NULL;
}

char* bee()
{
  return "I don't like you, short guy!";
}

char* choice()
{
  return foo() || bee();
}

int main()
{
  char* result = choice();
  
  if(result == NULL) {
     printf("GOt a null again");
     return 1;
  }

  printf("Horray! Succsex!");
  return 0;
}

CodePudding user response:

What you are trying to do, can be achieved by this,

char* choice() {
    char* ch = foo();
    if(ch) { // check if it is not NULL, then return that ptr
        return ch;
    }
    return bee(); // if foo() returned NULL, return the output of bee()
}

You can not always have similar syntax for two entirely different languages. They may be simple in one language but requires a little more effort in another.

CodePudding user response:

C doesn't have that feature which JavaScript has. The || operator will always return a bool and no other type. I suggest simply writing it out:

const char* choice() {
    const char* foo_string = foo();
    if (foo_string) {
        return foo_string;
    } else {
        return bee();
    }
}

Also, you may have noticed that I changed char* to const char*, which you should do when using string literals like you are in the bee() function.

CodePudding user response:

Solution 1.

Use an auxiliary variable:

char *res = NULL;
(res = foo()) || (res = bar()) || (res = tar()) || ...;
return res;

Note that assignment operator is used when one would expect a equality operator. The extra parentheses are used to silence warnings about using = is the condition.

Solution 2.

Assuming that you use GCC or CLANG compiler you can use following extension. This let's you use the value of condition as a return value of conditional operator by omitting operator's second argument:

char* choice()
{
  return foo() ?: bee(); // add ?: tar() ?: mar() ?: ...
}

It's pretty much exactly what you are looking for.

  • Related