发布时间:2022-08-09 文章分类:编程知识 投稿人:赵颖 字号: 默认 | | 超大 打印

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