strrchr help

This is a discussion on strrchr help within the C Programming forums, part of the General Programming Boards category; Hi guys I'm having trouble with the code below. I know strchr and strrchr both take int's as the second ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    10

    strrchr help

    Hi guys

    I'm having trouble with the code below.
    I know strchr and strrchr both take int's as the second argument but I don't know how to make it work the way I want.
    With strrchr I manually pass '/' as the second argument and it works.

    I've tried different int's & chars using typecasting but I can't make it work by passing a variable. I don't know why it takes an int when likely we are going to be searching for some chars?

    Here is the current code and error I get.

    Thanks so much for your help guys!
    lastoccurrence.c: In function `main':
    lastoccurrence.c:29: warning: passing arg 2 of `strchr' makes integer from pointer without a cast
    Code:
    int main(int argc, char *argv[])
    {
    char stringsearch[900];
    char *stringtosearch[900];
    char *result;
    /*printf("Argc=%d\n",argc);*/
    
      if( argc != 2 )
      {
          printf("WTF: You need to enter 1 arguments: The string to search in\n");
          return 39798;
      }
    
    strcpy(stringsearch,argv[1]);
    
    /*
    printf("String To Search=%s\n",stringtosearch);
    printf("ARG1 (string to search)=%s\n",stringsearch);
    */
    
    result=strrchr(stringsearch,'/');
    result=strchr(stringsearch,result);
    printf("%s\n",result);
    return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    char *result;
    
    ...
    
    result=strchr(stringsearch,result);
    If you know strchr() takes an int as a second parameter, why are you trying to stuff a char * down its throat? Square Peg, meet Round Hole. lol....

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    I know, that is my point and example.
    I know it can't work but there must be a way to accomplish what I want.

    I don't understand why I can pass it '/' manually and it works....because a slash is not an int.
    I want to somehow be able to pass a char variable instead as the second parameter.

    Is something like this possible?

    Otherwise I don't understand the point of the function if I can't make it dynamic by passing some sort of variable to it.

    Does anyone have a solution?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    A '/' can indeed be an int.

    Take a short time-out, and consider the difference between char and int and char *. The following information is based upon a 32-bit, x86 system. The numbers below are not to be considered valid for actual calculations that require 100% correct information.

    With that said......

    A char is a one byte variable. An int is a four byte variable. A char * is a four byte variable designed to hold the address of a char (which is only one byte). Human readable characters, mainly the English set, are able to be stored into a byte. This includes your '/' char. By convention, the '/' has a numerical representation of 47 (check an ASCII table). Such a number can be held in either a char or an int.

    This is why you can pass '/' to strchr() because you're just giving it a value of 47. Hence, it assumes you're sending it as an int, since it makes the most sense. It wants an int, and it knows a char can fit inside of an int, so it implicitly does the typecasting.

    A char *, however, is not quite related. It's a pointer to a char, although it's generally used to mean to be a pointer to a contiguous sequence of chars.

    '/' is not a string. It's not a char *. It's a simple char that can be viewed as an int.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Umm . . . like this?
    Code:
    char c = '/';
    const char *string = "/usr/bin/program";
    char *p = strrchr(string, c);
    You can't use a char*, you need to use a char.

    If you want to search for the next '/' in a string, add the previous position to the beginning of the string, plus one:
    Code:
    char dirsep = '/';
    const char *program = "/usr/bin/program";
    char *one = strchr(program, c);
    
    if(program[one-program]) {
        char *two = strchr(program + one-program + 1, c);
    }
    The if statement prevents the +1 running off the end of the string if the string was, say, "/usr/".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Thanks guys for your help and wisdom.
    I think I need to re-evaluate how I want to accomplish what I want to accomplish.

    I guess basically what I'm trying to do is:
    Take the result of "result=strrchr(stringsearch,'/');"
    And then remove that portion from the rest of the original string.

    I'll think about it and come back and post either way

    Thanks again.

    You guys are all geniuses, good at explaining things and friendly....I couldn't ask for a better community

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    result in this case will just be a pointer to the location where the char was found. If such a char was found, then take everything before it, and everything after it and make it a new concatenated string.

    If you need to keep the buffer, then you could just shift everything after the char in question over one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-28-2009, 09:30 AM
  2. Counting characters
    By Calavera in forum C Programming
    Replies: 5
    Last Post: 10-01-2004, 10:15 AM

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