c - C 编程中使用 strstr 函数时指针的使用

标签 c

我有以下程序

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char line1[30],line2[30],*chp;
puts("Enter line1:");
gets(line1);
puts("Enter Line2");
gets(line2);
chp=strstr(line1,line2);
if(chp)
printf("%s String is present in Given String",line2);
else
printf("%s String is not present in Given String",line2);
getche();
}

我知道chp在这个程序中是一个指针,我怀疑chp会存储一个内存位置,但是字符串(line2)的内存位置是如何存储在chp指针中的,请程序员帮助我理解这个程序。

最佳答案

strstr函数在s1指向的字符串中搜索s2指向的字符串。 它返回一个指向 s1 中第一次出现 s2 的指针。

因此它只存储一个内存位置,指向在 line1 中找到 line2 的位置的指针,否则为 null..

关于c - C 编程中使用 strstr 函数时指针的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18247163/

相关文章:

c - C : Binary does not receive when using addrinfo 中的多播

c - 在 C 中的 if 语句后提示用户?

c - C中的printf()和puts()有什么区别?

c - 如何计算未知 int 类型的范围?

检查字母数字中字符是大写还是小写

php - 从 C 返回一个 char*,但在 PHP 中返回空

c++ - 将共享库转换为静态库?

c - 在 C 中查找结构的定义

c - 如何在 EOF 或 '\n' 时退出循环?

c++ - 编码最佳实践