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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步