Why O why, strlen?

This is a discussion on Why O why, strlen? within the C Programming forums, part of the General Programming Boards category; Why is it that strlen returns the number of chars currently in the string, and not the actual declared size ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701

    Why O why, strlen?

    Why is it that strlen returns the number of chars currently in the string, and not the actual declared size of the array?

    When I first started using strlen, I loved this feature, since when used to control a loop it avoids useless iteration. Now I want something that will tell me the declared size of the array, regardless of how many chars are currently in it...

    And since I'm on the subject, how might one dinamically allocate a string of certain size? I know that you cannot place a variable in the [array_size] area, only a straight number else a #defined real number. So how?
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    lol

    Um, duh. You declared it, why don't you know it...and if you don't know it for some odd reason, what's wrong with a while loop?

    while(array[x] != '\0')
    x++;
    x++; //so you get the very last char (for the '\0')

    dynamic allocation is simple...

    char *array;

    array = (char *)malloc(size);

    where size is how big you want it...

    hth

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    That will only tell him what strlen() would. If you're working with an array and not a pointer then

    yourarray/yourarray[0];

    will give you the size of the array.

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    ohh...*smacks himself*

    no...it'd give him one+strlen ...I haven't programmed in 3 days! gimme a break!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    O.K.

    Here's the scoop:

    I'm trying to write a function that emulates fgets(), but is used like gets()...
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    >>I haven't programmed in 3 days! gimme a break!<<

    That's no excuse. It should be longer than a week before you forget anything .

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* newgets(char* buffer,int num)
    {
    int i;
    fgets(buffer,num,stdin);
    i=strlen(buffer)-1;
    if (buffer[i]=='\n') buffer[i]='\0';
    return buffer;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    alex
    Guest
    Code:
    #include <stdio.h>
          
    #define MYGETS_BASESIZE 8
          
    char *mygets(char *buffer)
    {
       int s=0, l=0;
    
       do /* read a long line in small pieces */
       {
          s+=MYGETS_BASESIZE;
          buffer=(char *)realloc(buffer, s);
          fgets(&buffer[l], s-l, stdin);
          l+=strlen(&buffer[l]);
       }
       while(buffer[l-1]!='\n');
    
       buffer[l-1]='\0';   
       buffer=(char *)realloc(buffer, l);
       
       return buffer;
    }
          
    main()
    {   
       char *input;
    
       /* read first line */
       input=mygets(NULL);
       printf("first line: %s\n", input);
    
       /* read another line, 'overwriting' the first line */
       input=mygets(input);
       printf("second line: %s\n", input);
        
       /* free malloc'ed memory */
       free(input);
    }
    The mygets function automatically adjusts the string to the right size. This also means that you cannot use a static buffer as argument to this function, and that you are responsible for freeing the allocated memory!

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    Wowsers! Ya didn't have to write it out for me!

    But no complaints!

    Thanks Alex, thanks Stony!
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  10. #10
    Goran
    Guest

    Wink

    To get the actual declared size of the array you can use

    sizeof(arrayname);

    sizeof returns the actual declared array size regardless of the number of characters it contains.

    Hope that solves your original query.

    -Goran.

  11. #11
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    and....

    sizeof(arrayname)/sizeof(arrayname[0]) will give you the number of elements in the array regardless of the data type the array contains...

    mxr
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    Thanks again guys!
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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