Reputation: 48268
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:
index
name outside of scope for eg - or worse, not warn and use the function in a way that's not intended.-Wshadow
if you have a variable called index
.See:
Notes:
index
shouldn't be redefined since libraries may use it.index
.deprecated
attribute, so warnings related to using deprecated functions have no effect.Upvotes: 9
Views: 374
Reputation: 41055
If I understand well, you want to use index
as a variable name without conflicting with the index
function included in <strings.h>
.
In this case you can override using the preprocessor:
#include <stdio.h>
#define index(s, c) deprecated_index(s, c)
#include <strings.h>
int index = 5;
int main(void)
{
printf("%d\n", index);
return 0;
}
Or override index
with strchr
:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define index(s, c) strchr(s, c)
int main(void)
{
int index = 5;
char *ptr = index("text", 'x');
printf("%s %d\n", ptr, index);
return 0;
}
Upvotes: 0
Reputation: 141613
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]
Upvotes: 6
Reputation: 134076
You shouldn't redefine these identifiers, as some library that you're linking against could still depend on them existing.
Upvotes: 2