The difference between sizeof and strlen

1. The difference between sizeof and strlen

sizeof is a keyword, and the parameters can be various data (including functions, types, objects, arrays, pointers…) used to calculate the byte size of the data.

strlen is a function. The parameter type must be a character pointer (char *). It is used to calculate strings. It starts traversing from the first address of the string until it encounters ‘\0’.

2. sizeof (character array), strlen (character array)

sizeof (character array):

int main()
{
    char arr[] = { 'a','b','c','d','e','f' };

    printf("%zd\
", sizeof(arr));//6 Here arr represents the entire array, the entire array is 6 characters, and the total number of bytes is 6
    printf("%zd\
", sizeof(arr + 0));//4/8 where arr + 0 is the address of the first element, and the size of the address is 4 or 8 bytes
    printf("%zd\
", sizeof(*arr));//1 Here *arr is the first element, which is the character a, and the size is 1 byte
    printf("%zd\
", sizeof(arr[1]));//1 where arr[1] is the character b and the size is 1 byte
    printf("%zd\
", sizeof( & amp;arr));//4/8 Here & amp;arr is the address of the entire character array, and the size of the address is 4 or 8 bytes
    printf("%zd\
", sizeof( & amp;arr + 1));//4/8 Here & amp;arr + 1 is the address of the entire character array + 1, which is still an address, and the size of the address is 4 or 8 bytes
    printf("%zd\
", sizeof( & amp;arr[0] + 1));//4/8 Here & amp;arr[0] + 1 is the address of character a + 1, which is still the address , the address is 4 or 8 bytes
    return 0;
}

strlen (character array):

int main()
{
    char arr[] = { 'a','b','c','d','e','f' };

    printf("%d\
", strlen(arr));//Random value where arr represents the first character address and is also a character pointer, which meets the parameter requirements. strlen stops when it encounters \0. There is no \0 in the array, so it is random value
    printf("%d\
", strlen(arr + 0));//Random value here arr + 0 represents the first character address + 0, which is still the first character address and a character pointer, meeting the parameter requirements, strlen encounters \0 stops, there is no \0 in the array, hence the random values
    //printf("%d\
", strlen(*arr));//Error report here *arr is the character a, which does not meet the parameter requirements
                                              //(From the perspective of the strlen function, the function will treat any data passed in as a character pointer, that is, an address. The ASCII code value of character a is 97.97 is accessed directly as an address. This behavior is illegal. , error reporting)
   // printf("%d\
", strlen(arr[1]));//error report here arr[1] is the character b, which does not meet the parameter requirements
                                              //(From the perspective of the strlen function, the function will treat any data passed in as a character pointer, that is, an address. The ASCII code value of character a is 97.97 is accessed directly as an address. This behavior is illegal. , error reporting)
    printf("%d\
", strlen( & amp;arr));//Random value where & amp;arr is the address of the entire character array, which meets the parameter requirements, but I don’t know when \0 will be encountered, so is a random value
    printf("%d\
", strlen( & amp;arr + 1));//Random value here & amp;arr is the address of the entire array + 1. It is still an address and meets the parameter requirements, but I don’t know when \0 will be encountered, so it is a random value
    printf("%d\
", strlen( & amp;arr[0] + 1));//Random value here & amp;arr[0] + 1 is the address of character a + 1, which is still the address, It meets the parameter requirements, but I don’t know when \0 will be encountered, so it is a random value.
    return 0;
}

3. sizeof (string), strlen (string)

sizeof(string):

int main()
{
    char arr[] = "abcdef";
    printf("%zd\
", sizeof(arr));//7 Here arr is the entire array (string). In addition to abcdef, the string also has \0 at the end, so there are 7 bytes in total.
    printf("%zd\
", sizeof(arr + 0));//4/8 Here arr + 0 is the first element address + 0, the result is still the first element address, the address occupies 4/8 bytes
    printf("%zd\
", sizeof(*arr));//1 where *arr is the character a and the size is 1 byte
    printf("%zd\
", sizeof(arr[1]));//1 where arr[1] is the character a and the size is 1 byte
    printf("%zd\
", sizeof( & amp;arr));//4/8 where & amp;arr is the address of the entire string, and the size of the address is 4/8 bytes
    printf("%zd\
", sizeof( & amp;arr + 1));//4/8 Here & amp;arr + 1 is the address of the entire string + 1, which is still the address and the size of the address is 4/8 bytes
    printf("%zd\
", sizeof( & amp;arr[0] + 1));//4/8 Here & amp;arr[0] + 1 is the address of character a + 1, which is still the address , the size of the address is 4/8 bytes

    return 0;
}

strlen (string):

int main()
{
    char arr[] = "abcdef";
    printf("%d\
", strlen(arr));//6 Here arr is the first character address, that is, the address of character a, which meets the strlen function parameter requirements. The traversal starts from the starting address and stops when it encounters \0. 6 bytes in total
    printf("%d\
", strlen(arr + 0));//6 Here arr + 0 is the first character address + 0, and the result is still the address of the first character a, which meets the strlen function parameter requirements, starting from The address starts traversing and stops when it encounters \0, a total of 6 bytes
    //printf("%d\
", strlen(*arr));//Error report here *arr is the character a, which does not meet the strlen function parameter requirements
                                 // From the perspective of the strlen function, it will treat any data passed to it as an address. The ASCII code value of character a is 97. Direct access of 97 as an address must be illegal.
    //printf("%d\
", strlen(arr[1]));//Error report here arr[1] is the character b, which does not meet the strlen function parameter requirements
                                   // From the perspective of the strlen function, it will treat any data passed to it as an address. The ASCII code value of character b is 98. Direct access of 98 as an address must be illegal.
    printf("%d\
", strlen( & amp;arr));//6 Here & amp;arr is the address of the entire string, which meets the strlen function parameter requirements, the address of the entire string and the address of the first character The same, so the strlen function will take the first character address as the starting address, and start traversing from here, a total of 6 characters, a total of 6 bytes
    printf("%d\
", strlen( & amp;arr + 1));//Random value here & amp;arr + 1 is the address of the entire string + 1, and the result is the address after \0, strlen The function uses this address as the starting address and starts traversing from behind \0. It cannot be sure when \0 will be encountered again, so it is a random value.
    printf("%d\
", strlen( & amp;arr[0] + 1));//5 Here & amp;arr[0] + 1 is the address of character a + 1, and the result is the address of character b address, then the strlen function will use the address of character b as the starting address, and start traversing from here, a total of 5 characters, a total of 5 bytes
    return 0;
}

4. sizeof (string pointer), strlen (string pointer)

sizeof (string pointer):

int main()
{
char* p = "abcdef";//The string is also an expression, its value is the address of the first character, that is, the address of character a
\t
printf("%d\
", sizeof(p));//4/8 where p is the address of character a, and the size of the address is 4 or 8 bytes
printf("%d\
", sizeof(p + 1));//4/8 Here p + 1 is the address of character a + 1, the result is still an address, the size of the address is 4 or 8 bytes
printf("%d\
", sizeof(*p));//1 Here *p is character a, and the size of character a is 1 byte
printf("%d\
", sizeof(p[0]));//1 Here p[0] is character a, and the size of character a is 1 byte
printf("%d\
", sizeof( & amp;p));//4/8 Here & amp;p takes the address of the pointer variable p, and the size of the address is 4 or 8 bytes
printf("%d\
", sizeof( & amp;p + 1));//4/8 Here & amp;p + 1 is the address of the pointer variable p + 1, the result is still the address, the size of the address is 4 or 8 bytes
printf("%d\
", sizeof( & amp;p[0] + 1));//4/8 Here & amp;p[0] + 1 is the address of character a + 1, and the result is character The address of b, the size of the address is 4 or 8 bytes

return 0;
}

strlen (string pointer):

int main()
{
    char* p = "abcdef";//The string is also an expression, its value is the address of the first character, that is, the address of character a

    printf("%d\
", strlen(p));//6 Here p is the address of character a, which meets the parameter requirements of strlen function. The address of character a is used as the starting address to traverse backward until it encounters \ Stop at 0, 6 characters in total
    printf("%d\
", strlen(p + 1));//5 Here p + 1 is the address of character a + 1, and the result is the address of character b, which meets the requirements of strlen function parameters. The address is used as the starting address and traversed backward until it encounters \0 and stops, a total of 5 characters.
    //printf("%d\
", strlen(*p));//Report an error. Here *p is the character a, which does not meet the parameter requirements of the strlen function. An error is reported.
    //printf("%d\
", strlen(p[0]));//Error report, here p[0] is the character a, which does not meet the requirements of strlen function parameters
    printf("%d\
", strlen( & amp;p));//Random value here & amp;p is to take the address of the pointer variable p, and use the address of p as the starting address to traverse backward, but not Know when to encounter \0, so it is a random value
    printf("%d\
", strlen( & amp;p + 1));//Random value here & amp;p + 1 is the address of the pointer variable p + 1, and the secondary address is used as the starting address backwards Traverse, but don't know when \0 will be encountered, so random values
    printf("%d\
", strlen( & amp;p[0] + 1));//5 Here & amp;p[0] + 1 is the address of character a + 1, and the result is the address of character b Address, using the address of character b as the starting address, traverse backward until it encounters \0 and stops, a total of 5 characters

    return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 179,851 people are learning the system