strstr(buff, "via")
时间: 2023-08-10 18:03:08 浏览: 53
`strstr` 函数用于在一个字符串中查找指定子字符串的第一次出现位置。如果找到了子字符串,则返回该位置的指针;如果未找到,则返回空指针。
在你给出的例子中,`strstr(buff, "via")` 是在字符数组 `buff` 中查找子字符串 "via" 的第一次出现位置。
以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char buff[] = "30.30.30.0/24 via 172.17.17.2 dev nas0_0 metric 1";
char* position = strstr(buff, "via");
if (position != NULL) {
printf("The substring 'via' is found at position: %ld\n", position - buff);
} else {
printf("The substring 'via' is not found.\n");
}
return 0;
}
```
在这个例子中,我们定义了一个字符数组 `buff`,并使用 `strstr` 函数查找子字符串 "via" 的位置。如果找到了该子字符串,我们计算并打印它在 `buff` 中的位置;否则,输出未找到的消息。
运行这段代码,你将会看到输出结果为:The substring 'via' is found at position: 13。这表示子字符串 "via" 在 `buff` 中的位置是从索引 13 开始的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)