Confused with following code
I'm trying to implement my own strstr() function but my knowledge of pointers is quite fuzzy.
char *str = "The quick brown fox jumped over the lazy dogs.";
p = strstr(str, "lazy");
printf("%s\n", p); // "lazy dogs."
// p is NULL after this, since the string "wombat" isn't in str:
p = strstr(str, "wombat");
First of all I'm confused with the first line lol.
Within C programming I've been using arrays to manipulate strings. With this initialization, I'm assuming it sets a pointer to that string.
Here's where I have trouble with pointers. When I first tried to use how strstr() I saw
on elook.org I thought that the parameters should be pointers but that didn't make sense and did something like char *strstr("asdfg","asdf"); First of all I failed to realize that I should have assigned it to a pointer variable and second there shouldn't be a * on the strstr function. I seem to have trouble with pointers.
char *strstr( const char *str1, const char *str2 );
I don't really understand the address and pointers. When I first thought of strstr I was thinking that it was going to literally return the address of str2 in str1 and got confused with that. With the printf of "lazy dogs" I'm guessing that it prints whatever the pointer is pointing at until the end of the text.
I guess I should really read up on pointers and addresses.