How to get the length of array of strings in C? Yahoo! Answers

How to get the length of array of strings in C? - Yahoo! Answers

Best Answer - Chosen by Voters

cja gave you one solution. In case you have a local array defintion,

you can determine the number of elements using the sizeof operator.

But what to do, in case you pass an array as a parameter to another

function without telling that function how many elements it had:

int fct(char **array)

{

int howmany = -1;

}

The solution for this problem is to NULL terminate the array you are using:

i.e.:

char *ar[] = {

"Hello World!",

"Good Night World!",

"Here I sleep!",

0

};



now what you can do is this:

int fct(char **array)

{

char *ptr = array[0];

int iCount = 0;

while(*ptr) {

++iCount; // increments element counter

++ptr; // points to the next element.

}

return iCount;

}

Please note: this does not only work for strings, but for all type of objects (typically you would use a different terminator for numb
原文地址:https://www.cnblogs.com/lexus/p/2578674.html