为什么我们不在 main 函数的 strstr 中传递指针参数

why we dont pass pointer arguments in strstr in main function

char *strstr(char *string2, char string*1)

strstr 函数中,参数是指针字符串,但是当我们从 main.. 传递参数时,为什么我们只使用字符串而不使用它们的地址?

#include <stdio.h>
#include <string.h>
main()
{
   char s1 [] = "My House is small";
   char s2 [] = "My Car is green";

   printf ("Returned String 1: %s\n", strstr (s1, "House"));
   printf ("Returned String 2: %s\n", strstr (s2, "Car"));
}

s1 和 s2 是(或可以用作)指针。它们都指向字符串的第一个字符。

这里是c标准的文字

6.3.2.1 Lvalues, arrays, and function designators
...
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

我应该提一下,您可以定义一个字符串。

char* s1 = "My House is small";

但这与

不同
char s1 [] = "My House is small";

因为 char* s1 将在一些包含字符串文字的只读内存中定义,而 char s1 [] 将被定义为堆栈上的静态数组。