man pages section 3: Basic Library Functions

Exit Print View

Updated: July 2014



index, rindex - string operations


#include <strings.h>

char *index(const char *s, int c);
char *rindex(const char *s, int c);


The index() and rindex() functions operate on null-terminated strings.

The index() function returns a pointer to the first occurrence of character c in string s.

The rindex() function returns a pointer to the last occurrence of character c in string s.

Both index() and rindex() return a null pointer if c does not occur in the string. The null character terminating a string is considered to be part of the string.


On most modern computer systems, you can not use a null pointer to indicate a null string. A null pointer is an error and results in an abort of the program. If you wish to indicate a null string, you must use a pointer that points to an explicit null string. On some machines and with some implementations of the C programming language, a null pointer, if dereferenced, would yield a null string. Though often used, this practice is not always portable. Programmers using a null pointer to represent an empty string should be aware of this portability issue. Even on machines where dereferencing a null pointer does not cause an abort of the program, it does not necessarily yield a null string.


See attributes(5) for descriptions of the following attributes:

Interface Stability

See also

bstring(3C), malloc(3C), string(3C), attributes(5), standards(5)