Find the size of a string pointed by a pointer

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

    if ptr length is an argument of a function it's reasonable to use pointers as a strings. we can get string length by following code:

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

    And for more explanation, if string is an input string by user with variable length, we can use following code:

    unsigned char *ptr;
    ptr=(unsigned char *)calloc(50, sizeof(unsigned char));
    scanf("%s",ptr );
    length=strlen((const char *)ptr);
    

提交回复
热议问题