Reputation: 533
I have declared an array of 30 elements as:-
unsigned char some_array[30];
I store some elements in the array, for eg:-
{0xFB, 0x07, 0x0B, 0x02, 0x00, 0x0C, 0x03, 0x08, 0x0F, 0x0F, 0x07, 0x0A, 0x09, 0x06, 0x0D}
I need to find the number of elements in this array.
sizeof (some_array) / sizeof (some_array[0]);
yields 30, which is the total number of elements the array can store.
strlen(some_array);
yields 4, which is the index of 0x00 element in the array.
How can i determine the number of elements stored in the array ??
Upvotes: 0
Views: 236
Reputation: 516
In C there is no in build function to find number of elements stored in an array, because all unfilled fields in array will have some value. It can only able to find total size of array.
One way to do this is to fill entire array with a predefined special character before loading values to array (which do not include special character). For example;
unsigned char some_array[30];
memcpy(some_array, '\0', 30);
Then load some values to array;
strlen(some_array);
Here strlen(some_array);
will return number of characters in array
Upvotes: 1
Reputation: 4035
Before assigning the actual values, you can do a memset
on the entire array which would set a value you are sure won't be part of your initialized range of values, example 0xff. Then do a for
loop and count.
int cnt = 0;
memset (some_array, 0xff, 30);
for (i = 0; i < 30; i++) {
if (some_array[i] != 0xff) {
cnt++;
}
}
printf ("%d\n", cnt);
Upvotes: 0
Reputation: 126
There is no built-in C construct to do what you are asking.
As you said, C can tell you how large you've allocated the array to be. However, it has no concept of when you store a meaningful value in the array. When you declare the array, it has 30 (probably garbage-valued) elements in it, and when you put some of your own values in it, it still has 30 elements, some of which are your values and some of which are garbage.
strlen()
works on strings, a special type of char
array in C which stores a null byte to mark the end of the string.
Upvotes: 2
Reputation: 40145
define macro like this:
#define INITIALIZE_LIST {0xFB, 0x07, 0x0B, 0x02, 0x00, 0x0C, 0x03, 0x08, 0x0F, 0x0F, 0x07, 0x0A, 0x09, 0x06, 0x0D}
#define INITIALIZE_LIST_SIZE sizeof((unsigned char[])INITIALIZE_LIST)
sample code to use :
#include <stdio.h>
#define INITIALIZE_LIST {0xFB, 0x07, 0x0B, 0x02, 0x00, 0x0C, 0x03, 0x08, 0x0F, 0x0F, 0x07, 0x0A, 0x09, 0x06, 0x0D}
#define INITIALIZE_LIST_SIZE sizeof((unsigned char[])INITIALIZE_LIST)
int main(void){
unsigned char some_array[30] = INITIALIZE_LIST;
size_t size = INITIALIZE_LIST_SIZE;
printf("Number of initialize list is %zu\n", size);
for(size_t i = 0; i < size; ++i)
printf("%02X ", some_array[i]);
return 0;
}
Upvotes: 2
Reputation: 29266
If you define an element as "anything except 0x00" then a for loop will do it.
int len = sizeof (some_array) / sizeof (some_array[0]);
int count = 0;
for(int i = 0 ; i < len; i++)
{
if (some_array[i] != 0x00) count++;
}
/* count is now the number of non 0 elements */
Based on your comment it looks like you have a 30 element array but only populate the first 15 and want 15 as "the answer". The array still has 30 elements, no way (that I know of) to determine from the array that you only put 15 values into it.
Upvotes: 2
Reputation: 263217
The number of elements in the array is 30.
If you want to know the number of elements to which you've assigned values, you'll have to keep track explicitly. There's no way to distinguish between an array element with some assigned value, and an uninitialized element whose (garbage) value happens to be that same value.
Upvotes: 2