Thread: strlen()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    err: undefined :(
    Join Date
    Mar 2005
    Posts
    17
    yeah, that does work, but it wasn't in the specific way I was using it.

    I had to do this... which still is a little flakey...

    ( key is a pointer pointing to a var that has a string in it passed through a function ).

    Code:
    char cKey[256];
    
      for( int i=0;i<strlen(key);i++ )
      cKey[i] = *(key+i);

    then i could do:


    Code:
    maxCKey = strlen( cKey );
    hah.

    I guess if for some reason anyone else gets in to the situation I did, that's one way of doing it.


    edit:
    in the line: for( int i=0;i<strlen(key);i++ )

    sizeof(key) doesn't make all of the key show
    and strlen(key) loops way past actual key and makes funky symbols show.

    lol. awesome.
    Last edited by exoeight; 04-01-2005 at 08:23 AM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by exoeight
    yeah, that does work, but it wasn't in the specific way I was using it.

    I had to do this... which still is a little flakey...

    ( key is a pointer pointing to a var that has a string in it passed through a function ).

    Code:
    char cKey[256];
    
      for( int i=0;i<strlen(key);i++ )
      cKey[ i ] = *(key+i);

    then i could do:


    Code:
    maxCKey = strlen( cKey );
    hah.



    I guess if for some reason anyone else gets in to the situation I did, that's one way of doing it.


    edit:
    in the line: for( int i=0;i<strlen(key);i++ )

    sizeof(key) doesn't make all of the key show
    and strlen(key) loops way past actual key and makes funky symbols show.

    lol. awesome.
    First of all: you do realize that there is no such thing as a "string" data type in C, right?

    All standard C library "string" functions (strlen(), strcpy(), etc., including printf("%s")) operate on a null-terminated sequence of chars.

    1. The argument for strlen is (const *char), not (const char).

    2. If you use the name of an array of char in an expression, it is treated as a pointer to char.

    3. Your loop copies all of the chars up to but not including the terminating zero byte, so the result won't be useable with any standard C library string functions, including printf("%s"). So, your loop doesn't loop "way past the actual key", but trying to print the result with %s could go on forever.

    4. If key points to a null-terminated sequence of chars and the the number of chars (including the terminating null) is not greater than the size of the char array cKey, then you can use this to copy the key into your array:
    Code:
      strcpy(cKey, key)
    Regards,

    Dave

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by exoeight
    Code:
    char cKey[256];
    
      for( int i=0;i<strlen(key);i++ )
      cKey[i] = *(key+i);
    then i could do:
    Code:
    maxCKey = strlen( cKey );
    The first looks like a homebrewed strcpy except for the null termination that would make the subsequent strlen work correctly.
    Last edited by Dave_Sinkula; 04-01-2005 at 09:14 AM. Reason: D'oh!
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  5. strlen
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 11:57 PM