Conditionally set a variable if it's NULL

后端 未结 2 1920
无人及你
无人及你 2021-01-21 23:16

When stepping through a sqlite3_stmt, I\'d like to check against a return value of NULL rather than store it and check against the stored value.

<
2条回答
  •  被撕碎了的回忆
    2021-01-21 23:58

    A GNU extension to C allows the ternary conditional operator to evaluate to its condition if it's true and there's nothing in the first branch:

    char *email = (char *)sqlite3_column_text(statement, 10);
    email = email ? : "";
    

    or, more exactly what you say you're looking for

    char * email = (char *)sqlite3_column_text(statement, 10) ? : "";
    

    This works when compiling with Clang, too.

    Another possibility would be:

    char *email = (char *)sqlite3_column_text(statement, 10);
    email = (NULL == email) ? email : "";
    

    But I think I'd recommend just going with your first option. Make a code snippet if you're doing it a lot.

提交回复
热议问题