pistacchio
pistacchio

Reputation: 58883

Length of an array of pointers

if I have an array of pointers like char **lines, how can i determine its length? Thanks

Upvotes: 3

Views: 2184

Answers (5)

Cholavendhan
Cholavendhan

Reputation: 347

Getting the length of a pointer to an array

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

Sanjaya R
Sanjaya R

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

Jonathan Leffler
Jonathan Leffler

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

sth
sth

Reputation: 229593

You can't. You have to manually keep track of the length of arrays.

Upvotes: 7

anon
anon

Reputation:

That's not an array of pointers, it's a pointer to a pointer.

Upvotes: 1

Related Questions