I have a char**
(a null terminate array: last element of array is NULL) returned by a function (which I can not change):
char** ptar = get_ptar();
I want to iterate over it and pass its value to another function (again which I can not change):
collection(int index, char* str);
I have this code so far:
int I = 0;
while (*ptar != 0)
{
collection(i, (char*)(ptar));
ptar++; i++;
}
But it passes garbage value.
Is there a better approach get string out of a null-terminated array?
Related question:
Is there a way to get the length of char** ptar
using C++11's:
std::char_traits<?>::length
Try the following:
for ( int i = 0; *( ptar + i ) != 0; i++ )
{
collection( i, *( ptar + i ) );
}
Or
for ( int i = 0; ptar[i] != 0; i++ )
{
collection( i, ptar[i] );
}
In C++ 2011 instead of integral constant 0 as null pointer expression you may use nullptr
To find the length of ptar
you can do this:
int n = 0;
while ( ptar[n] ) ++n;