Export (0) Print
Expand All
div
eof
Expand Minimize
1 out of 1 rated this helpful - Rate this topic

strrchr, wcsrchr, _mbsrchr, _mbsrchr_l

Scan a string for the last occurrence of a character.

char *strrchr(
   const char *str,
   int c 
); // C only
char *strrchr(
   char *str,
   int c 
); // C++ only
const char *strrchr(
   const char *str,
   int c 
); // C++ only
wchar_t *wcsrchr(
   const wchar_t *str,
   wchar_t c 
); // C only
wchar_t *wcsrchr(
   wchar_t *str,
   wchar_t c 
); // C++ only
const wchar_t *wcsrchr(
   const wchar_t *str,
   wchar_t c 
); // C++ only
unsigned char *_mbsrchr(
   const unsigned char *str,
   unsigned int c 
); // C only
unsigned char *_mbsrchr(
   unsigned char *str,
   unsigned int c 
); // C++ only
const unsigned char *_mbsrchr(
   const unsigned char *str,
   unsigned int c 
); // C++ only
unsigned char *_mbsrchr_l(
   const unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C only
unsigned char *_mbsrchr_l(
   unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C++ only
const unsigned char *_mbsrchr_l(
   const unsigned char *str,
   unsigned int c,
   _locale_t locale
); // C++ only
str

Null-terminated string to search.

c

Character to be located.

locale

Locale to use.

Returns a pointer to the last occurrence of c in str, or NULL if c is not found.

The strrchr function finds the last occurrence of c (converted to char) in str. The search includes the terminating null character.

wcsrchr and _mbsrchr are wide-character and multibyte-character versions of strrchr. The arguments and return value of wcsrchr are wide-character strings; those of _mbsrchr are multibyte-character strings.

In C, these functions take a const pointer for the first argument. In C++, two overloads are available. The overload taking a pointer to const returns a pointer to const; the version that takes a pointer to non-const returns a pointer to non-const. The macro _CONST_CORRECT_OVERLOADS is defined if both the const and non-const versions of these functions are available. If you require the non-const behavior for both C++ overloads, define the symbol _CONST_RETURN.

_mbsrchr validates its parameters. If str is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and _mbsrchr returns 0. strrchr and wcsrchr do not validate their parameters. These three functions behave identically otherwise.

The output value is affected by the setting of the LC_CTYPE category setting of the locale; for more information, see setlocale. The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. For more information, see Locale.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_tcsrchr

strrchr

_mbsrchr

wcsrchr

n/a

n/a

_mbsrchr_l

n/a

Routine

Required header

strrchr

<string.h>

wcsrchr

<string.h> or <wchar.h>

_mbsrchr, _mbsrchr_l

<mbstring.h>

For more information about compatibility, see Compatibility.

For an example of using strrchr, see strchr.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.