Find the size of a string pointed by a pointer

前端 未结 9 1008
一整个雨季
一整个雨季 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:10

    Use strlen to find the length of (number of characters in) a string

    const char *ptr = "stackoverflow";
    size_t length = strlen(ptr);
    

    Another minor point, note that ptr is a string literal (a pointer to const memory which cannot be modified). Its better practice to declare it as const to show this.

提交回复
热议问题