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

In strstr function the arguments are pointer strings but when we pass arguments from main.. Why do we use only strings but not their address?

#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"));
}
sozkul
  • 665
  • 4
  • 10

1 Answers1

0

s1 and s2 are (or can be used as) pointers. They both point to the first character of the string.

Here is the text from c standard

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.

I should mention that you could define a string with.

char* s1 = "My House is small";

But this would not be the same as

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

Because char* s1 would be defined in some read-only memory containing the string-literal and char s1 [] would be defined as a static array on the stack.

aebudak
  • 641
  • 6
  • 8