The difference between the usage of sizeof() and strlen()

The sizeof [operator] returns the length (in bytes) of a type or data object.

1. The sizeof of the array (indicates the length of the entire array, that is, the value in parentheses)

Note: 1. If you use sizeof for an array element , you get the length of the element.

  1. If you use sizeof for the array name, you will get the number of bytes in the entire array.

  2. When the [character array] represents a string , its sizeof value will be calculated with ‘/0’.

4. When the array is a formal parameter , its sizeof value is equivalent to the sizeof value of the pointer

Example 1:

char a[ 20 ];
    char b[]={ "abc" };
    char c[]={ 1 , 2 , 3 };
    cout << sizeof (a)<< endl ;      //1, array, the value is 20 
   cout << strlen (a)<< endl ;      //2, the value is 11 (this value is different in different compilers) 
   cout << sizeof (b)<< endl ;      //3, the string array, The value is 4 
   cout << strlen (b)<< endl ;      //4, the value is 3 
   cout << sizeof(c)<< endl ;      //5, array, the value is 3, note that because it is not a string array, '
char a[ 20 ];
char b[]={ "abc" };
char c[]={ 1 , 2 , 3 };
cout << sizeof (a)<< endl ;      //1, array, the value is 20 
cout << strlen (a)<< endl ;      //2, the value is 11 (this value is different in different compilers) 
cout << sizeof (b)<< endl ;      //3, the string array, The value is 4 
cout << strlen (b)<< endl ;      //4, the value is 3 
cout << sizeof(c)<< endl ;      //5, array, the value is 3, note that because it is not a string array, '\0' is not counted in 
cout << sizeof (b[ 0 ])<< endl ;   //6, Array element, value is 1 
cout << sizeof (c[ 1 ])<< endl ;   //7, array element, value is 1 
cout <<c[ 2 ]<< endl ;           //8, value is small heart shape
' is not counted in cout << sizeof (b[ 0 ])<< endl ; //6, Array element, value is 1 cout << sizeof (c[ 1 ])<< endl ; //7, array element, value is 1 cout <<c[ 2 ]<< endl ; //8, value is small heart shape

The difference between the usage of sizeof() and strlen(): the sizeof operator indicates the length of the entire array, but the strlen() function returns the length of the string stored in the array, not the length of the array itself. Also, strlen() only counts visible characters, not null characters. (see above for specific examples)

The function strlen() counts the number of characters from the first element of the array until a null character is encountered. For uninitialized data, the position of the first null character is random.

void  func ( char a[ 3 ])
 {
     int c = sizeof (a); //c = 4, because a is not an array type here, but a pointer, which is equivalent to char *a. 
}

Corresponds to the fourth point of the note above. 

Second, the sizeof of the pointer

The pointer is used to record the address of another object, so the memory size of the pointer is of course equal to the width of the computer’s internal address bus.

On a 32-bit computer, the return value of a pointer variable must be 4.

The sizeof value of a pointer variable has nothing to do with the object pointed to by the pointer.

3. The sizeof of the function

sizeof can also evaluate a function call , and the result is the size of the function’s return value type , and the function is not called .

The form of evaluating a function: sizeof(function name(argument list))

Note: 1) A function with a null return type cannot be evaluated. 

2) The function name cannot be evaluated.

3) For functions with parameters , when using sizeof, the actual parameter list must be written . example:

float FuncP(int a, float b)
{
    return a + b;
}

int FuncNP()
{
    return 3;
}

void Func()
{
}

int  main ()
 {
 cout << sizeof (FuncP( 3 , 0.4 ))<< endl ; //OK, the value is 4, sizeof(FuncP(3,0.4)) is equivalent to sizeof(float) 
cout << sizeof (FuncNP ())<< endl ; //OK, the value is 4, sizeof(FuncNP()) is equivalent to sizeof(int) 
/*cout<<sizeof(Func())<<endl; //error, sizeof cannot be returned Function evaluation of empty type*/ 
/*cout<<sizeof(FuncNP)<<endl; //error, sizeof cannot evaluate function name*/

Leave a Comment

Your email address will not be published. Required fields are marked *