Question: The strchr function

This is a discussion on Question: The strchr function within the C Programming forums, part of the General Programming Boards category; hey guys , this is the strchr function , i was wondering what does that return at the end of ...

  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
    21,310
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,159
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    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,159
    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
    21,310
    Refer to your notes on the correct way to use scanf to read a string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03: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, 08:10 AM

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