Reputation: 58883
if I have an array of pointers like char **lines, how can i determine its length? Thanks
Upvotes: 3
Views: 2184
Reputation: 347
char *array[]={"welcome","to","India"};
length=sizeof(array)/sizeof(array[0]);
Output:
3
we can get the length by dividing (size of pointer to array by size of single one)
Because there is no predefined function for getting length of array..
Upvotes: -1
Reputation: 6416
It depends on the data. If there is no associated count, it could be a NULL terminated list.
char** lines = mysteryfunction();
for ( ;*lines;lines++ ) {
printf( "%s\n", *list );
}
Upvotes: 1
Reputation: 753725
You can't reliably.
Sometimes, there is a null pointer marking the end - it is one convention sometimes used. More often, you need to be told the length.
But there is no fool-proof way of determining the length. You have to know (or be told) the length, somehow.
Upvotes: 4