char
is an integral type. It can be used as an index value for the []
operator. Note however that t['0']
is not the same element as t[0]
. The value of '0'
depends on the encoding used on the platform. Most environments use ASCII for the source and execution character sets, where '0'
has the value 48.
Indexing through character values is useful for many algorithms, especially searching and word matching. Typical implementations of the functions in <ctype.h>
use arrays of 257 entries (or sometimes 384 entries for safety) where the function argument is used as an index.
Yet there is a major problem in using char
values an index variables: the char
type can be signed or unsigned by default, so the range of its values can encompass negative values. In the code fragment, if t
is an array or a pointer to the beginning of an array, any character in p
with a negative value will cause an access outside the boundaries of the array, which has undefined behavior.
It is advisable to raise the warning level so the compiler diagnoses such uses that are well hidden potential bugs. Use gcc -Wall
or clang -Weverything
.
To avoid this potential problem, the code should be modified this way:
#define MAX 256
int t[MAX];
void shifttable(char p[]) {
int i, j, m;
m = strlen(p);
for (i = 0; i < MAX; i++)
t[i] = m;
for (j = 0; j < m - 1; j++)
t[(unsigned char)p[j]] = m - 1 - j;
}
Note also that i
, j
, m
and the t
array should have type size_t
to handle strings longer than INT_MAX
.