# C `strrchr()`函数

> 原文： [https://beginnersbook.com/2017/11/c-strrchr-function/](https://beginnersbook.com/2017/11/c-strrchr-function/)

**`strrchr()`函数**在给定字符串中搜索指定字符的最后一次出现。此函数与[函数`strchr()`](https://beginnersbook.com/2017/11/c-strchr-function/)完全相反，后者在字符串中搜索字符的第一次出现。

## C `strrchr()`函数声明

```c
char *strrchr(const char *str, int ch)
```

`str` - 搜索字符`ch`的字符串。

`ch` - 需要搜索的字符

## `strrchr()`的返回值

它返回指向字符串中字符的最后一次出现的指针。这意味着如果我们显示`strrchr()`的返回值，那么它应该显示从指定字符的最后一次出现开始的字符串部分。

## 示例：`strrchr()`函数

```c
#include <stdio.h>
#include <string.h>
int main () {
   const char str[] = "This-is-just-a-test-string"; 
   const char ch = '-'; 
   char *p, *p2;

   p = strrchr(str, ch); 
   printf("String starting from last occurrence of %c is: %s\n", ch, p);

   p2 = strrchr(str, 'i'); 
   printf("String starting from last occurrence of 'i' is: %s\n", p2);

   return 0;
}
```

输出：

```c
String starting from last occurrence of - is: -string
String starting from last occurrence of 'i' is: ing
```

#### 相关文章：

1.  [C - `strncpy()`示例](https://beginnersbook.com/2017/11/c-strncpy-function/)
2.  [C - `strcpy()`示例](https://beginnersbook.com/2017/11/c-strcpy-function/)
3.  [C - `strncmp()`示例](https://beginnersbook.com/2017/11/c-strncmp-function/)
4.  [C - `strchr()`示例](https://beginnersbook.com/2017/11/c-strchr-function/)