strstr()返回值
时间: 2023-11-04 18:58:29 浏览: 30
strstr()函数是C语言中的一个字符串处理函数,用于在一个字符串中查找另一个字符串的第一次出现位置。
它的函数原型如下:
```c
char *strstr(const char *haystack, const char *needle);
```
该函数接受两个参数,`haystack`和`needle`,分别表示要在其中查找的字符串和要查找的目标字符串。
返回值:
- 如果目标字符串存在于要查找的字符串中,则返回第一次出现的位置的指针。
- 如果目标字符串不存在于要查找的字符串中,则返回`NULL`。
相关问题
strstr 返回值
strstr函数返回的是str2在str1中首次出现的地址。当str1的值改变时,返回值也会随之改变。因此,在使用strstr返回值前,不要修改str1的值,以防止出错。
范例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "world";
char *ret;
ret = strstr(str1, str2);
printf("str2在str1中的地址:%p\n", ret); // 输出:0x7ffeeb3a4a0d
return 0;
}
```
strstr返回值、
函数 `strstr` 是 C 标准库中的一个字符串处理函数,其定义如下:
```c
char* strstr(const char* str1, const char* str2);
```
该函数用于在字符串 `str1` 中查找第一次出现字符串 `str2` 的位置,并返回该位置的指针。如果未找到字符串 `str2`,则返回 `NULL`。
例如,下面的代码将在字符串 `str1` 中查找字符串 `str2`:
```c
char* str1 = "hello, world";
char* str2 = "world";
char* result = strstr(str1, str2);
if (result != NULL) {
printf("'%s' is found in '%s'\n", str2, str1);
printf("The position of '%s' in '%s' is %ld\n", str2, str1, result - str1);
} else {
printf("'%s' is not found in '%s'\n", str2, str1);
}
```
输出结果为:
```
'world' is found in 'hello, world'
The position of 'world' in 'hello, world' is 7
```
这里的返回值是一个指向字符的指针,指向字符串 `str1` 中第一次出现字符串 `str2` 的位置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)