Several compiler vendors have implemented a non standard extension __attribute__((nonnull))
to specify that a pointer must not be a null pointer.
C99 introduced a new syntax to specify that a function argument must point to the first element of an array with at least a given number of elements, so passing a null pointer as argument s
to a function foo
declared as void foo(int s[static 1])
would be a constraint violation that the compiler can detect and report. Yet this syntax is restricted to function arguments so it cannot be used for function return values nor variable or aggregate member definitions. Furthermore passing a pointer one past the last element of an array to foo
would still be a constraint violation albeit not a null pointer. void foo(int s[static 0])
does not seem to be allowed by the C Standard.
There does not seem to be an attribute [[nonnull]]
in section 6.7.12 Attributes of the latest C23 draft (the only standard attributes are [[deprecated]]
, [[fallthrough]]
, [[maybe_unused]]
, [[nodiscard]]
, [[noreturn]]
, [[_Noreturn]]
, [[reproducible]]
and [[unsequenced]]
.
A similar but different question was asked before: Is __attribute__((nonnull)) standardized in C
The response is no but it does not answer my question.
Is there an alternative way in Standard C to specify that a pointer should not be null?
Was there a proposal in this direction for C23?