[In-depth analysis of C language] Explain the difference and usage of strlen and sizeof in detail

strlenAbout sizeofthe difference between and , I believe many people have some understanding

Below I will analyze in depth thestrlen differences and precautions between andsizeof

meaning

sizeof()It is an operator. When the type of the header file is unsigned int, its operation value is calculated at compile time. The parameters can be pointers, arrays, types, objects, functions , etc.;

strlen()is a function that can only be computed at runtime. The argument must be a character pointer (char*). When the array name is passed in as a parameter, the array actually degenerates into a pointer. The function completed by this function is to traverse from the first address representing the string until the terminator NULL is encountered. The returned length size does not include NULL.

Example 1

#include <stdio.h>
#include <string.h> //strlen要引头文件

int main()
{
    
    
    char str[20] = "hello";

    printf("strlen=%d\n", strlen(str));
    printf("sizeof=%d\n", sizeof(str));
    return 0;
}

The result is shown as:

strlen = 5
sizeof = 20

At this time strlen=5, sizeof=20

Because strlenthe length of the \0string end of the string;

The sizeofcalculation is the size of the memory space occupied by the allocated array str[20], which is not affected by the content stored in it.

Example 2

#include <stdio.h>

int main()
{
    
    
    char *str1 = "abcde";
    char str2[] = "abcde";
    char str3[8] = {
    
    'a'};
    char str4[] = "0123456789";

    printf("sizeof(str1)=%d\n", sizeof(*str1));
    printf("sizeof(str2)=%d\n", sizeof(str2));
    printf("sizeof(str3)=%d\n", sizeof(str3));
    printf("sizeof(str4)=%d\n", sizeof(str4));

    return 0;
}

The result is shown as:

sizeof(str1) = 4
sizeof(str2) = 6   
sizeof(str3) = 8   
sizeof(str4) = 11  

str1is a pointer, just pointing to a string "abcde". So it sizeof(*str1)'s not the space occupied by strings, nor the space occupied by character arrays, but the space occupied by a pointer . In C/C++ a pointer occupies four bytes .

str2Is a character array, for an array, returns the total space occupied by the array, so the total space sizeof(str2)of the string is obtained "abcde". "abcde", there are a b c d e \0six characters in total, so the length of the str2 array is 6. ·

str3It has been defined as an array of length 8, so it sizeof(str3)is 8;

str4and str2similar, there are eleven characters in total, so str4the space occupied is 11.

illustrate

Example 2 lists a pointer. If you verify my code, it may besizeof(str1) = 8

That's because the pointer size is 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine

Because the addressing address space of a 32-bit machine is 4G , each address is 32 bits, which is exactly 4 bytes. That is, the pointer size is 4 bytes.

On a 64-bit machine, each address is 64 bits , which is 8 bytes, so a pointer is 8 bytes.

code three

In the sub function , the character array ziseofpassed in from the main function is treated as a pointer .

The size of the pointer is determined by the machine, not artificially.

void size_of(char str[])
{
    
    
    printf("sizeof = %d\n", sizeof(str));
}

int main()
{
    
    
    char str[20] = "hello";

    size_of(str);

    return 0;
}

The result is shown as:

sizeof = 4

Specifically, when the parameters are as follows, the meaning of the value returned by sizeof is as follows:

Array: The size of the array space allocated at compile time;

Pointer: the size of the space used to store the pointer (the length of the address where the pointer is stored, which is a long integer and should be 4);

Type: the size of the space occupied by the type;

Object: the actual size of the space occupied by the object;

Function: The size of the space occupied by the return type of the function. The return type of a function cannot be void.

code four

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

int main()
{
    
    
	char *str = "0123456789";

	printf("sizeof(str) = %d\n", sizeof(str));

	printf("sizeof(*str) = %d\n", sizeof(*str));

	printf("strlen(str) = %d\n", strlen(str));

	return 0;
}

The result is shown as:

sizeof(str) = 4
sizeof(*str) = 1
strlen(str) = 10

sizeof(str): stris a character pointer that points to a string constant, and sizeofthe space occupied by the first pointer is obtained, which should be a long integer, so it is 4;

sizeof(*str): *stris the first character, in fact, the memory space occupied by the first character of the string is obtained ’0’, which is of char type and occupies 1 bit

sizeof(str): If you want to get the length of this string, you must usestrlen

Summarize

sizeofis an operator that measures the allocated size of characters

strlenis a function that measures the actual length of the character and \0ends with, so it ends as soon as it strlenhits\0

Guess you like

Origin blog.csdn.net/m0_63325890/article/details/121016274
Recommended