From cbf1018b21ea2a060a3825fbdb1453800b5b7320 Mon Sep 17 00:00:00 2001 From: lemon Date: Tue, 14 Apr 2026 18:40:42 +0200 Subject: c: make type of conditional expr with pointers match non void pointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Here's what the C standard[1] says about the conditional operator: ``` if one operand is a null pointer constant, the result has the type of the other operand; otherwise, one operand is a pointer to void or a qualified version of void, in which case the result type is a pointer to an appropriately qualified version of void. `` But actually, some codebases rely on things like 'x ? &foo.memb : NULL' to have the type of `&foo.memb`, and not `void *`... [1]: ISO/IEC 9899:TC3 ยง6.5.15 --- src/c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/c.c') diff --git a/src/c.c b/src/c.c index 2d51bd2..c6df58f 100644 --- a/src/c.c +++ b/src/c.c @@ -540,7 +540,7 @@ condtype(const Expr *a, const Expr *b) if (t1.t == TYPTR && t2.t == TYPTR) { s1 = typechild(t1), s2 = typechild(t2); if (s1.bits == s2.bits || s2.t == TYVOID || s1.t == TYVOID) { - return mkptrtype(s1.t == TYVOID ? s1 : s2, (t1.flag | t2.flag) & TFCHLDQUAL); + return mkptrtype(s1.t == TYVOID ? s2 : s1, (t1.flag | t2.flag) & TFCHLDQUAL); } } return mktype(0); -- cgit v1.2.3