strpbrk()

This is a discussion on strpbrk() within the C Programming forums, part of the General Programming Boards category; if i have Code: char * ptr = NULL; ptr= strpbrk(str, "-%"); /* how do i write an if statement ...

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

  3. #3
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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

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