Thread: pointers and strlen

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    pointers and strlen

    Hello
    I have this code
    Code:
    int mystrlen(char *string) {
            
            int len;
            char *p = string;
    
    	for(; string[0] != '\0'; string++)
                           ;
    len = p - &string[0];
    return len;
    }
    It works but i dont how. I mean string[0] != '\0' will be always true if the string is > 0 and it is. How can len be the lenght if p points to the begining of string? I suppose p points wherever string does, right?
    I was wondering how can i write this function without pointer p..

    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I was wondering how can i write this function without pointer p.
    Code:
    int mystrlen(const char *s)
    {
       int len = 0;
       while ( *s++ != '\0' )
       {
          ++len;
       }
       return len;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or do this:
    Code:
    int mystrlen(const char *s)
    {
       int len = 0;
       while ( s[len++] != '\0' );
    
       return len-1; /*edit*/
    }
    Or just do that:
    Code:
    int mystrlen( const char *s )
    {
        return *s == '\0' ? 0 : 1 + mystrlen( s+1 );
    }
    Or do...

    Quzah.
    Last edited by quzah; 08-23-2003 at 05:47 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    quzah, your first version doesn't work properly

    Another "or do":
    Code:
    int mystrlen(const char *s)
    {
       const char *p = s;
       while (*p) p++;
       return p - s;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    quzah, your first version doesn't work properly

    Another "or do":
    Yeah but at least it fit the criteria.

    Oh, and technicly, to fit criteria, you could rename your pointer 'q' instead of 'p' and do it "without pointer p".

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but at least it fits the criteria
    Doh, I didn't even bother reading that
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's another way:
    Code:
    int mystrlen( const char *s )
    {
        return strlen(s);
    }


    #define mystrlen strlen

    But now I'm just being ridicules.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by quzah
    Here's another way:
    Code:
    int mystrlen( const char *s )
    {
        return strlen(s);
    }
    I knew someone would show this one -- I was afraid it was going to be me! Thanks for beating me to it, Q
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >len = p - &string[0];

    I was getting negative numbers using the above.
    len = &string[0] - p;

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Hammer, your code should be:
    Code:
    int mystrlen(char *s)
    {
       const char *p = s;
       while(*s++ && *s);
       return s - p;
    }
    Last edited by cc0d3r; 08-25-2003 at 02:24 PM.
    $ENV: FreeBSD, gcc, emacs

  11. #11
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Originally posted by cc0d3r
    Hammer, your code should be:
    Code:
    int mystrlen(char *s)
    {
       const char *p = s;
       while(*s++ && *s);
       return s - p;
    }
    both his and your code works , but his while loop is different

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Hammer, your code should be:<<
    Why exactly? What is the purpose of the two tests in your code?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Never mind
    $ENV: FreeBSD, gcc, emacs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By saszew in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 03:30 AM
  2. String Pointers
    By mbuster in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 09:39 AM
  3. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  4. cstring algorithm.. what do you think?
    By The Brain in forum C++ Programming
    Replies: 6
    Last Post: 01-26-2005, 02:54 AM