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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。