Link to home
Start Free TrialLog in
Avatar of Wookie68
Wookie68Flag for United States of America

asked on

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

Avatar of ozo
ozo
Flag of United States of America image

char * my_strchr(const char *s, int c){
   while( *s && *s != c ){
       s++;
   }
   if( *s == c ){ return (char *)s; }
   return (char *)0;
}
Avatar of Wookie68

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!