小编Dav*_*erb的帖子

如何从指针中减去字符数组?

所以,我开始熟悉 C,在这一点上我试图理解指针。我从这里得到以下代码,但我无法理解,如何从指针中减去字符数组。

#include<stdio.h>
#include<string.h>
#include<conio.h>

main() 
{   
char s[30], t[20];   
char *found; 

/* Entering the main string */   
puts("Enter the first string: ");   
gets(s);

/* Entering the string whose position or index to be displayed */   
puts("Enter the string to be searched: ");   
gets(t);

/*Searching string t in string s */   
found=strstr(s,t);   
if(found)
    printf("Second String is found in the First String at %d position.\n",found-s);    
else
    printf("-1");   
getch(); 
}
Run Code Online (Sandbox Code Playgroud)

指针不只是给定变量/常量的地址吗?当减法发生时,字符数组会自动假设,因为操作是用指针发生的,所以减去它的地址?我在这里有点困惑。

提前致谢。

c arrays pointers pointer-arithmetic implicit-conversion

2
推荐指数
1
解决办法
92
查看次数