How to get the real and total length of char * (char array)?

前端 未结 15 2522
小蘑菇
小蘑菇 2020-11-29 00:37

For a char [], I can easily get its length by:

char a[] = \"aaaaa\";
int length = sizeof(a)/sizeof(char); // length=6

However,

15条回答
  •  再見小時候
    2020-11-29 01:30

    You can find the length of a char* string like this:

    char* mystring = "Hello World";
    int length = sprintf(mystring, "%s", mystring);
    

    sprintf() prints mystring onto itself, and returns the number of characters printed.

提交回复
热议问题