Measuring the length

This is a discussion on Measuring the length within the C Programming forums, part of the General Programming Boards category; Is there a way to measure an unsigned buffer thats half full and half empty. Code: unsigned char buffer[100]; int ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Measuring the length

    Is there a way to measure an unsigned buffer thats half full and half empty.
    Code:
    unsigned char buffer[100];
    int i;
    for(i=0; i<50; i++){
     buffer[i] = rand%5;
     }
    Is there a function to call to measure the length of buffer so that the lenght is 50 instead of 100

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Yes:
    Code:
    #define BUF_LEN 100
    unsigned char buffer[BUF_LEN];
    int i, buflen = 0;
    
    for (i = 0; i < 50; i++){
            buffer[i] = rand%5;
    }
    buf_len = i;
    
    printf("Current length of buffer = %i.\n", buf_len);
    Last edited by Kennedy; 09-11-2009 at 10:18 AM. Reason: Removed the simicolon after my #define ::bows head in shame::

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I was originally going to suggest strlen, but that will give a much smaller answer since there's almost certainly a zero character before the end.

    Otherwise, it's your job to know how many items you've put in an array, so storing it in a variable is the way to go.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    @Kennedy: no semicolons after #defines!
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kiros88 View Post
    Is there a way to measure an unsigned buffer thats half full and half empty.
    How do you know if it's half full? Answer that, and you've solved your problem.


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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Code:
    int optimist ( unsigned char buffer[] ) {
      return HALF_FULL;
    }
    
    int pessimist ( unsigned char buffer[] ) {
      return HALF_EMPTY;
    }
    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.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by dwks View Post
    @Kennedy: no semicolons after #defines!
    Crap. . . I was in a hurry. I isn't like I compiled that or such.

    EDIT: Though I know what I did was wrong, your statement isn't correct either:
    Quote Originally Posted by include/linux/spinlock_up.h
    Code:
    #define __raw_read_trylock(lock)  ({ (void)(lock); 1; })
    Last edited by Kennedy; 09-11-2009 at 10:22 AM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    No, you see, that's *part* of the #define. I stated that one should not put semicolons after #defines, i.e., as the last character of the definition. :P

    But never mind, my statement wasn't supposed to hold in general.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 08:38 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 01:04 PM
  5. Measuring string length
    By Zaarin in forum C++ Programming
    Replies: 13
    Last Post: 09-12-2001, 10:20 PM

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