Thread: strpbrk()

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    strpbrk()

    if i have
    Code:
    char * ptr = NULL;
    ptr= strpbrk(str, "-%");
    /* how do i write an if statement checking if ptr found a - or a % and is pointing to it?*/
    if(ptr == '%')
     printf("this isnt working.. could someone help me?\n");

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First you need
    if ( ptr != NULL )

    Then you can look at *ptr to find which char it is pointing to
    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.

  3. #3
    Quote Originally Posted by paperbox005
    if i have
    Code:
    char * ptr = NULL;
    ptr= strpbrk(str, "-%");
    /* how do i write an if statement checking if ptr found a - or a % and is pointing to it?*/
    if(ptr == '%')
     printf("this isnt working.. could someone help me?\n");
    What does your manual say?

    http://www.neosoft.com/neosoft/man/strpbrk.3.html

    The strpbrk() function locates in the null-terminated string s the first occurrence of any character in the string charset and returns a pointer to this character. If no characters from charset occur anywhere in s strpbrk() returns NULL.
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    yes i know it returns a pointer, but how do i do a string compare with a pointer and a character?>

  5. #5
    Quote Originally Posted by paperbox005
    yes i know it returns a pointer,
    Actually, it returns an address that it is useful to store in a pointer of type char.
    but how do i do a string compare with a pointer and a character?>
    Why a string compare? You want a character compare. Use '*' to dereference the pointer and '==' to compare. Nothing really new here... Do your best. I want to see your code.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    if( *ptr == '%' )

  7. #7
    Quote Originally Posted by paperbox005
    if( *ptr == '%' )
    Good! If you have more choices, also consider switch-case.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone double check and maybe improve my code
    By tommy69 in forum C Programming
    Replies: 23
    Last Post: 04-21-2004, 02:04 PM