const char *const _Nonnull GetString() {
...
}
Is that cross platform? Or is _Nonnull
either a compiler extension or a c99 feature?
const char *const _Nonnull GetString() {
...
}
Is that cross platform? Or is _Nonnull
either a compiler extension or a c99 feature?
The _Nonnull
token is not cross-platform, or standard. It's something that Apple used to use in its ObjectiveC compilers to trigger additional compile-time checking of the variable declared as _Nonnull
. More modern versions of the language have upgraded this macro to a first-class keyword.
In this use, I believe the compiler will make sure that the function doesn't explicitly return a NULL-pointer value. (It's not perfect, though, as code can always misuse pointer-arithmetic to return NULL at runtime, something the compiler cannot detect)
I've never seen this used in standard C, only ever in old ObjectiveC.
For C99, the static
keyword can be used to enforce non-NULL inputs to functions, although the syntax will make you wonder what's going on at first:
int processNonNullString(char myString[static 1])
{
/* ... */
}
This use of static
before the array size (1), tells the compiler that 'myString' is a character array of at least one element. Remember that arrays are pointers in C, so this restriction rules out the possibility of the pointer being null.