How to find the length of an char array in c

后端 未结 6 1167
野性不改
野性不改 2021-02-06 12:06

I want to find the length of this :

char *s[]={\"s\",\"a\",\"b\"};

it should count 4 with the /0 but the strlen or sizeof(s)/sizeof(char) gives

6条回答
  •  醉梦人生
    2021-02-06 12:28

    strlen works if you terminate your array with null character. You cannot find number of elements in a char array unless you keep track of it. i.e store it in some variable like n. Every time you add member increment n and every time you remove decrement n

提交回复
热议问题