I would think there is a library function to find the number of elements in an array of pointers- such as:
char xx[] = {"abc", "aaoo", "bb", ......."x"}
but at the moment am stumped.
:(
Printable View
I would think there is a library function to find the number of elements in an array of pointers- such as:
char xx[] = {"abc", "aaoo", "bb", ......."x"}
but at the moment am stumped.
:(
If the array declaration is visible to your code, you can use the sizeof operator, dividing the size of the array by the size of the element type, e.g.Quote:
Originally posted by Sirjorj
I would think there is a library function to find the number of elements in an array of pointers- such as:
(Note that the declaration as you posted it with char xx[] is incorrect, as you initialize the array with char pointers, not chars).Code:char* xx[] = {"abc", "aaoo", "bb", ......."x"};
int elementCount = sizeof(xx) / sizeof(char*);
However, if you just have a pointer to the first element (for example, when you pass the array to a function as a poniter or reference) you're out of luck - plain C arrays don't carry information about their size, so there can't be a library function which finds it out.
In C++, you would rather use a std::vector, which can be queried about the current number of elements.
Hi i am simpleman
i think the above solution is the perfect.
if it is not char Pointer type array (which means that the element's size is not fixed..), what about using m_nArrayCount variable..
for examplem, you can declare one integer variable for the element count of the array and increase the counter when you add an element into the array, and decrease the counter when you delete an element from the array.. ^^
What's the point of using an OO language if you refuse to reuse the code. Adding a m_nCount variable and managing it would duplicate some ( as in very litle) of the std::vector.Quote:
Hi i am simpleman
i think the above solution is the perfect.
if it is not char Pointer type array (which means that the element's size is not fixed..), what about using m_nArrayCount variable..
for examplem, you can declare one integer variable for the element count of the array and increase the counter when you add an element into the array, and decrease the counter when you delete an element from the array.. ^^
However if you don't want to use vector ( some strange vow against using STL) you could use an ending mark in the array, such as:
Code:char* xx[] = {"abc", "aaoo", "bb", ......."x", NULL};
int elementCount = 0;
for( char** pIt = xx; *pIt != NULL; ++pIt)
{
++elementCount;
}