nullability annotations in C
nullability annotations in C
Posted Feb 12, 2025 8:41 UTC (Wed) by alx.manpages (subscriber, #145117)In reply to: nullability annotations in C by interalia
Parent article: Maintainer opinions on Rust-for-Linux
Any compiler that supports C11 should be able to support these.
Here's an example of how to write such a const-generic API:
```
alx@devuan:~/tmp$ cat strchr.c
const char *my_const_strchr(const char *s, int c);
char *my_nonconst_strchr(char *s, int c);
#define my_strchr(s, c) \
( \
_Generic(s, \
char *: my_nonconst_strchr, \
void *: my_nonconst_strchr, \
const char *: my_const_strchr, \
const void *: my_const_strchr \
)(s, c) \
)
alx@devuan:~/tmp$ gcc -Wall -Wextra -pedantic -S -std=c11 strchr.c
alx@devuan:~/tmp$
```