Olaf said:
I want to know how many integers has 'array'. (int *array)
Or chars has 'array' (char *array)
Etc...
So...
while (array) { ... } ????
while (array != EOF) {...} ????
while (array != NULL) {...} ???
(I want to use the minimun possible variables)
If you have the array definition in scope:
int a[10];
size_t asize;
...
/*
* Divide the total number of bytes used by a
* by the number of bytes used in a single
* element of a
*/
asize = sizeof a / sizeof a[0];
asize contains the number of elements (10) in the array.
Note that this *will not* work if the array is passed as a function
parameter; because of how C treats arrays, what actually gets passed is
a pointer to the first element of the function. If you're passing an
array to a function, you must also pass the number of elements in the
array as a separate parameter.
One workaround for this is to wrap an array in a struct type and pass
the struct as the parameter; it's not as convenient as it sounds,
however.
If you've declared the array dynamically through malloc() or calloc(),
you have to keep track of the number of elements you allocated.