strchr() function

This is a discussion on strchr() function within the C Programming forums, part of the General Programming Boards category; umm guys i don't understand this function....could someone explain to me how it works?? and how it returns a pointer ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    strchr() function

    umm guys i don't understand this function....could someone explain to me how it works??
    and how it returns a pointer value...thanks

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    umm thanks to that reply....sorry i don't get that....do you know where could i find the details of that function?? i mean i could see all the contents of that function...so that i could understand...thanks a lot...

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, the GNU Standard C library is open source (glibc) -> http://www.gnu.org/software/libc/

    Of course that's just one implementation... other libraries/systems may do it differently but *should* work exactly the same -- according to the standards.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    It's just a while loop that
    - compares with the character you passed
    - compares with \0
    It stops when it reaches either one.

    What else do you need to know - it's about as basic as it gets when it comes to string processing.

    The only thing lower than this is strlen(), have you figured out that one yet?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      const char *s = "Hello world! I am a big string to use to show what strchr() does.";
      const char *t = s;
    
      for(;t; t = strchr(t, ' '))
        printf("&#37;s\n", t);
      return 0;
    }
    What does it look like it is doing?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I prefer to write for-loops like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      const char *s = "Hello world! I am a big string to use to show what strchr() does.";
      const char *t;
    
      for(t = s;t; t = strchr(t, ' '))
        printf("%s\n", t);
      return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As do I, however I do like initializing my variables up front so I just ended up writing my code like that. So... in a nutshell, duly noted my nit-picking friend.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    Thanks a lot.....Thank you3x.....

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are welcome, sick. I hope you understand how useful strchr() can be now. Unlike strtok() it doesn't alter your input data. There is a function strrchr() that I use for a lot of things... like parsing out paths. It does the same thing as strchr() but it starts from the end and works its way forward.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    thanks again master 5001 i really appreciate your help guys.....umm master 5001 do you have some messengers account? cause i really need some expert on c to guide on learning and i respect your knowledge about in c programming...i you would like to share your messenger account you could pm it to me...or if you do not like it's fine to me....thanks again so much

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah its on my profile thingy. My msn is master5001 _at_ hotmail _dot_ com... I am only publicly posting it on the thread because of the fact that it used to be publicly viewable to anyone... I am not seeing it on my profile anymore, though it is listed on my user control panel. Whatever. My contact info is not top secret.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    thanks again master5001..i appreciate it

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,659
    The examples will loop forever unless you adjust the token pointer, because it will keep finding the same part of the string. As simple as t++; really.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21