Thread: Question: The strchr function

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    Question: The strchr function

    hey guys , this is the strchr function , i was wondering what does that return at the end of the code do ? i can't really understand it , what does the '?' sign do ?

    Code:
     #include <string.h>
     /* strchr */
     char *(strchr)(const char *s, int c)
     {
         while (*s != '\0' && *s != (char)c)
             s++;
         return ( (*s == c) ? (char *) s : NULL );
     }

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    could someone put a main function in the code so i can trace it and try to get it myself ?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by everyone0
    what does that return at the end of the code do ? i can't really understand it , what does the '?' sign do ?
    Basically, it is another way of saying:
    Code:
    if (*s == c)
    {
        return (char *) s;
    }
    else
    {
        return NULL;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    mm thanks , the instructor definitely did not tell me that

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    giving me an expected error
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
     void strchr(*s, char c)
     {
         while (*s != '\0' && *s != (char)c)
             s++;
         return ( (*s == c) ? (char *) s : NULL );
     }
     main()
     {
     char s[5],c;
     scanf("%s",&s);
     strchr(&s,c);
     printf("%s",s);
     getch();
     }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why did you change from char* to void?
    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.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    still same problem when i turned it back to char*

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    strchr(*s, char c)
    Where do you specify the type for s? I don't understand your problem. You posted the correct code at the very beginning of this thread and now you're having trouble replicating it? Are you just not paying attention or what? Are you even trying?
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Gawking at stupidity
    If you don't stop that, your face is going to get frozen that way!

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    well i corrected the code as u said but now it says ambiguity between the function and the way im calling it
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
     char *(strchr)(const char *s, char c)
     {
         while (*s != '\0' && *s != (char)c)
             s++;
         return ( (*s == c) ? (char *) s : NULL );
     }
     main()
     {
     char s[5],c;
     scanf("%s",&s);
     strchr(s,c);
     printf("%s",s);
     getch();
     }

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's because there's already a prototype for a strchr function in string.h, which you're #including. Change the name of your implementation to my_strchr, or fake_strchr, or something instead to avoid the conflict.
    If you understand what you're doing, you're not learning anything.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to your notes on the correct way to use scanf to read a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    wow i feel stupid! thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Question: Can a function be nameless?
    By Omega Metroid in forum C++ Programming
    Replies: 20
    Last Post: 09-29-2009, 05:48 PM
  3. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM