我有以下程序
#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/