There are old functions such as index, rindex which have now been superseded by strchr and strrchr.
Is there a way to configure the compiler or defines so these functions aren’t available?
It can cause confusing warnings when:
- accidentally using
indexname outside of scope for eg – or worse, not warn and use the function in a way that’s not intended. - Older GCC versions (4.x) warn when using
-Wshadowif you have a variable calledindex.
See:
- http://www.gnu.org/software/libc/manual/html_node/Search-Functions.html#index-index
- http://pubs.opengroup.org/onlinepubs/009695399/functions/index.html
Notes:
- as @antti-haapala says, the global symbol
indexshouldn’t be redefined since libraries may use it.
This question is regarding the common case when a local variable is calledindex. - At the time of writing, glibc doesn’t mark these functions with the
deprecatedattribute, so warnings related to using deprecated functions have no effect.
Advertisement
Answer
Use the compiler in ISO C mode. The C standard prohibits conforming programs being broken by the presence of identifiers that are not reserved words.
For example, use flags -std=c99.
Sample program:
#include <string.h>
int main()
{
index("abc", 'x');
}
Compiled with -std=c11 -Werror gives:
error: implicit declaration of function 'index' [-Werror=implicit-function-declaration]