Find the size of a string pointed by a pointer

前端 未结 9 1057
一整个雨季
一整个雨季 2020-12-08 20:39
#include 

int main ()
{
    char *ptr = \"stackoverflow\"

}

Is there any way to find the length of stackoverflow pointed by ptr, a

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 21:05

    Purely using pointers you can use pointer arithmetic:

    int strLen(char *s)
    {
        int *p = s;
        while(*p !=’\0’)
        {
            p++;  /* increase the address until the end */
        }
        Return p – s; /* Subtract the two addresses, end - start */
    }
    

提交回复
热议问题