Re: Number of strings in an array.
goodTweetieBird <goodTweetieBird@hotmail.com> wrote:
The following snippet works because I know that there are three
strings in the array. How would one dynamically determine the number
of variable length strings in the array?
char* arr[] = {"string1", "string22","string333"};
#define count_of(a) (sizeof((a)) / sizeof((a)[0]))
int count = count_of(arr);
Works for arrays of all types (but unfortunately also compiles when
given a pointer, producing meaningless result). Some libraries use a
fancier macro, along these lines:
template <typename T, size_t N>
char (&array_size_helper(T (&arr)[N]))[N];
#define array_size(a) sizeof(array_size_helper((a)))
This fails to compile when given anything other than an array.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925