有条件设置一个变量,如果它是NULL [英] Conditionally set a variable if it's NULL
问题描述
在通过步进一个 sqlite3_stmt
,我想核对 NULL
,而不是商店的返回值它和检查对存储的值。
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.
下面是我现在在做什么:
Here's what I'm doing now:
char *email = (char *)sqlite3_column_text(statement, 10);
if (email == NULL) email = "";
和这里是我想的如的是做,减去双呼叫列
:
And here is what I'd like to be doing, minus the double-call to column
:
char *email = ((char *)sqlite3_column_text(statement, 10)) ? (char *)sqlite3_column_text(statement, 10) : "";
有没有一些方法来恩preSS第二前pression更简洁?我必须做很多重复这些,所以我在寻找短暂。
Is there some way to express the second expression more concisely? I have to do a lot of these repetitively, so I'm looking for brevity.
我知道这是控制流的问题,而不是用SQLite的API本身的问题,但你有它。我想不出一个好办法做到这一点。
I know this is a problem of control flow rather than a problem with the SQLite API itself but there you have it. I can't think of a good way to do this.
推荐答案
一个的 GNU扩展到C 使三元条件运算符来评估自身情况,如果这是真的,并没有什么第一分公司:
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) ? : "";
这铿锵编译时也有效。
另一种可能性是:
char *email = (char *)sqlite3_column_text(statement, 10);
email = (NULL == email) ? email : "";
不过,我想我会建议只是你的第一个选项去。做一个code片段,如果你做了很多。
But I think I'd recommend just going with your first option. Make a code snippet if you're doing it a lot.
这篇关于有条件设置一个变量,如果它是NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!