The difference between sizeof and strlen

1 The difference between sizeof and strlen

1. sizeof is an operator that does not need to reference the header file. The form is generally sizeof (variable or expression), and it calculates the occupied space. The units are bytes, so it can calculate the size of any type;

2. strlen is a string function and needs to reference the header file , and it can only calculate the size of characters in bytes. It cannot be used to calculate the size of other types. When encountering ‘\0’ will stop.

2 sizeof character array

#include <stdio.h>

void fun(char str[]) // What is passed is an address, which is the first address of the array
{
    printf("%d\\
", sizeof(str)); //8 calculates the address of the first element, depending on the number of digits of the computer 4/8
    return;
}

int main()
{
    /*
     * The address of the first element of the array arr represents the address of the first element of the array
     * The first address of the array &arr represents the address of the entire array
     */
    char arr[] = "hello world";

    /*
     * sizeof(arr) represents the size of the entire array
     * sizeof(arr + 0) represents the address of the first element of the array, depending on the number of digits of the computer 4/8 bits
     */
    printf("%d\\
", sizeof(arr)); //12 calculates the size of the entire array (string), and the result is 1*12=12

    fun(arr); //The address of the first element of the array is passed

    printf("%d\\
", sizeof(arr + 0)); //8 calculates the address of the first element, depending on the number of digits of the computer 4/8 bits

    printf("%d\\
", sizeof(arr + 1)); //8 arr + 1 represents the address of the second element. Since it is an address, it is 4/8 bytes

    printf("%d\\
", sizeof(*arr)); //1 *arr first element, the type of the first element is char, so it is 1

    printf("%d\\
", sizeof(*(arr + 1))); //1 arr + 1 is the address of arr[1], *(arr + 1) is the address of arr[1] The dereference represents the second element of the array, which is of type char and has a size of 1
    printf("%c\\
", *(arr + 1));// e

    printf("%d\\
", sizeof( & amp;arr)); //8 & amp;arr represents the address of the entire array, so it is 4/8 of the number of digits of the computer

    printf("%d\\
", sizeof( & amp;arr[0])); //8 & amp;arr[0] represents the address of the first element

    printf("%d\\
", sizeof( & amp;*arr)); //8 *arr represents the first element of the array, & amp;*arr represents the address of the first element, 4/ 8

    printf("%d\\
", sizeof(* & amp;arr));//12 & amp;arr represents the address of the entire array, * & amp;arr represents dereferencing the entire array, indicating the entire array the size of

    printf("%d\\
", sizeof( & amp;arr + 1));// & amp;arr represents the address of the entire array, & amp;arr + 1 represents skipping the address of 1 array size , or address

    printf("%d\\
", sizeof(*arr + 1)); //4 *arr represents the first element, *arr + 1 represents the first element increasing by 1, so it is of int type, so it is 4 words Festival
    printf("%c, %d\\
", *arr + 1, *arr + 1);// i, 105 char type data plus int type data are automatically converted to int type

    printf("%d\\
", sizeof( & amp;arr[0] + 1)); //8 represents the address of the second element of the array

    printf("%d\\
", & amp;arr[0]);//The address of the first element 6422036
    printf("%d\\
", & amp;arr[0] + 1);//The address of the second element 6422037

    return 0;
}

sizeof of 3 character pointers

char *p = “abcdef” defines a pointer to accept a string constant, where p only accepts the address of the first element of the string constant. Instead of storing the address of the entire string (because it is a char type, people can’t store such a long one, so they can only store one). In fact, it is enough to just remember the address of the first element, because it can be found through the address of the first element. The entire string (because the address of the string is also stored continuously).

#include <stdio.h>

int main()
{
    char *p = "abcdef"; //p is a character pointer pointing to the string "abcdef"

    printf("%d\\
", sizeof(p));//8 p is a character pointer, indicating the first address of the string, so it depends on the number of digits in the computer 4/8

    printf("%d\\
", sizeof(p + 1));//8 The address of the second element of the string

    printf("%d\\
", sizeof(*p)); //1 *p represents the character 'a', type is char

    printf("%d\\
", sizeof(p[0])); //1 p[0] represents the first element, character 'a'

    printf("%d\\
", sizeof( & amp;p)); //8 & amp;p is the address of character pointer p

    printf("%d\\
", sizeof( & amp;p + 1)); //The next address of the 8 array

    printf("%d\\
", & amp;p);// 6422024

    printf("%d\\
", & amp;p + 1); // 6422032

    printf("%d\\
", sizeof( & amp;p[0])); //8 Address of the first element

    printf("%d\\
", sizeof( & amp;p[0] + 1)); //8 The address of the second element

    return 0;
}

4 strlen character array application

4.1 Character array with ‘\0’

#include <stdio.h>
#include <string.h>

int main()
{
    char arr[] = "hello world";

    char arr1[] = { 'a','b','c','d','f' };

    printf("%d\\
", strlen(arr)); //11 starts from the first address and stops at the end '\0', so the result is 11

    printf("%d\\
", strlen(arr + 0)); //11 rr + 0 represents the address of the first element, and the result is still 11
 
    // printf("%d\\
", strlen(*arr)); // Error The formal parameter of strlen function is a pointer. It is illegal to pass array elements to strlen function.

    // printf("%d\\
", strlen(arr[1])); // Error The formal parameter of the strlen function is a pointer, and it is illegal to pass array elements to the strlen function.

    /*
     * 11 &arr takes out the address of the entire array, so its type is the array pointer type char(*)[12],
     * The type of strlen is char*. When running, a warning will be reported that the type is incompatible, so it is forced to be type-converted to char*.
     * Because it is the address of the array, the size is 11 bytes
     * */
    printf("%d\\
", strlen((char *) & amp;arr)); // 11
    
    /*
     * Here &arr is the first address of the array, &arr + 1 skips the entire array,
     * strlen will stop only when it encounters '\0', because we don’t know when it will encounter it later, so it is random.
     */
    printf("%d\\
", strlen((char *) & amp;arr + 1)); // random value

    printf("%d\\
", strlen( & amp;arr[0] + 1)); //10 & amp;arr[0] + 1 takes out the address of the second element of the array

    return 0;
}

4.2 Character array without ‘\0’

For arrays without ‘\0’, strlen will calculate random values (which is the premise of the address). If they are also passed elements, an error will occur.

#include<stdio.h>
#include<string.h>


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

    /*
     * For arrays without '\0', strlen will be used to find random values (which is the premise of the address). If they are also passed elements, an error will occur.
     * The following three outputs are all random values
     */
printf("%d\\
", strlen(arr));
 
printf("%d\\
", strlen(arr + 0));
 
printf("%d\\
", strlen((char *) & amp;arr));
 
// printf("%d\\
", strlen(*arr)); //This is wrong to write. What is passed in is not the address, but the first element of the array.
 
// printf("%d\\
", strlen(arr[1])); // Same as above
 
return 0;
}

5 strlen character pointer application

#include <stdio.h>
#include <string.h>

int main()
{
    char *p = "abcdef";

    printf("%d\\
", strlen(p)); //6 Starting from the position pointed to by p, look backward to the \0 position, 6 bytes apart.

    printf("%d\\
", strlen(p + 1)); // 5 Start from the position pointed to by p + 1, and look backward to the position \0. The two are separated by 5 bytes

    // printf("%d\\
", strlen(*p)); //error report

    // printf("%d\\
", strlen(p[0])); //error report

    printf("%d\\
", p); //4210688
    printf("%d\\
", & amp;p); //6422040

    printf("%d\\
", strlen((char *) & amp;p));//Random value &p is a secondary pointer, I don’t know where there is \0 behind the pointer, random value

    printf("%d\\
", strlen((char *) &p + 1));//Random value &p + 1 is a secondary pointer, compared to &p movement The size of a first-level pointer is 4 or 8 bytes. I don’t know where there is \0 after the pointer, a random value.

    printf("%d\\
", strlen( & amp;p[0] + 1));//5 & amp;p[0] is the address of the first element, + 1 is the address of the second element , this position is separated by 5 bytes from the following \0
    return 0;
}