Getting the size of the char* array passed as an input to C++ function

I am trying to pass a character pointer array as an input from C test driver to C++ function (available in C wrapper)

char* LowerList[3] = { "abc", "def", "hij" };

When available on the C++ side the values displayed for the the element is displayed as

char* LowerList[0]="abc";
LowerList[1]="def";
LowerList[2]="hij";
LowerList[3]=ƒì¶S<\$ UV<35?Ä@
LowerList[34]=ƒ<bad pointer>

I need to get the size of the array passed on to the C++ size which I could have got using while (LowerList[count])loop but unable to do so because of the junk value on the 4th position. Please let me know if any way I can find the correct size of the char* LowerList[] by initialising ,memory allocation or converting it to vector.

I am able to get the size of the array if its specified as

char* LowerList[4 or above ] = { "abc", "def", "hij" };

Also Is there a way I can initialise char* LowerList[10] in my C test driver without specifying the size as 10 as the code in the C wrapper accessing C++ function or I can do something to this array on the C++ side(e.g. realloc,vector etc) to get a temination value and get its size ingnoring the junk values

Updated on 7/15:Adding a NULL(sentinel value) or passing the size of the array is out of scope of my scenario.
I am trying a handle a case where user has done the declaration on the C side as
char* LowerList[3] = { "abc", "def", "hij" }; and I have to handle this on C++ side.
If someone can suggest this handling on the C++ side then its of great help for me as the C area is a grey one for me and it upto the user to give any inputs as long as C test driver compilation is a success.
Last edited on
Three solutions.

1) Pass the size as an additional parameter to the function.

2) Add a "sentinel" value the end of the array -- a NULL pointer. So
char* LowerList[] = { "abc", "def", "hij", 0 }; then loop until you get to a NULL.

3) Templates:
1
2
3
4
5
template< typename T, size_t N >
void do_it( T (&array)[ N ] ) {
    for( size_t i = 0; i < N; ++i )
        std::cout << array[ i ] << std::endl;
}


jsmith's #2 is a good and simple one and MS ATL's choice.
MS ATL as in MS Active Template Library ? It is still around in current times ? COM and DCOM seem so obsolete nowadays :P
Yes, Active Template Library. Why does it seem obsolete to you? The newest OS additions are COM-based, like Credential Providers. COM rules, simply put.
COM is very actively used in all versions of windows operating system. There are APIs only available trough COM interface (for example creating/resolving a shortcut)
Topic archived. No new replies allowed.