Create my_strchr function using pointers

While still on my conquest to understand pointers, I'd like to try and create a function to copy what the STL strchr () would do using pointers within my_strchr ().  

Thanks

Wookie68Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ozoConnect With a Mentor Commented:
The difference is that I declared s as const char *s instead of char * so I had to convert it before returning a char *
0
 
ozoCommented:
char * my_strchr(const char *s, int c){
   while( *s && *s != c ){
       s++;
   }
   if( *s == c ){ return (char *)s; }
   return (char *)0;
}
0
 
Wookie68Author Commented:
Hello ozo -
This is what I have so far and it appears to be working...

char * StrChar(char * s, char c)
{
      while(*s && *s != c)
      {
            s++;
      }
      if(*s == c)
      {
            return *s;
      }
      
      return (char * ) 0;
}

For your if statement you have, { return (char *)s; } Can you tell me the difference in your line and mine above in the if branch? Thanks

0
 
Wookie68Author Commented:
Thanks for the help and explanation. Pointers has been a concept that has been hard to grasp, but I figure the more I play with the code, the more I will learn it.

Thanks!
0
All Courses

From novice to tech pro — start learning today.