1. ## 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. 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);```

3. 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. @Kennedy: no semicolons after #defines!

5. Originally Posted by kiros88
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.

6. Code:
```int optimist ( unsigned char buffer[] ) {
return HALF_FULL;
}

int pessimist ( unsigned char buffer[] ) {
return HALF_EMPTY;
}```

7. Originally Posted by dwks
@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:
Originally Posted by include/linux/spinlock_up.h
Code:
`#define __raw_read_trylock(lock)  ({ (void)(lock); 1; })`

8. 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.