The use of sizeof operator in C and the difference between it and strlen

  • 2020-04-02 03:11:44
  • OfStack

I. definition:

Sizeof is an operator in C/C++ that simply returns the number of bytes an object or type takes up in memory. The return value is of type size_t, defined in the header file stddef.h. In a 32-bit system:

Char has a sizeof value of 1. Char is the smallest data type we can program.

The sizeof short is 2;
Int, float, and long are 4;
Double is 8;
All Pointers have sizeof value of 4.

Ii. Grammar:

Sizeof has three grammatical forms, as follows:

1) the sizeof (object); // sizeof;
2) the sizeof (the type_name); // sizeof;
) the sizeof the object; // sizeof object;
 
The sizeof (2); // 2 is of type int, so it is equivalent to sizeof(int);
The sizeof (2 + 3.14); // 3.14 is of type double, and 2 is also promoted to double, so it is equivalent to sizeof(double);
 
Iii. Illustration:


char* ss = "0123456789";
sizeof(ss)  The results of  4  = = =" ss Is a character pointer to a string constant 
sizeof(*ss)  The results of  1  = = =" *ss Is the first character 
char ss[] = "0123456789";
sizeof(ss)  The results of  11  = = =" ss It's an array. It's calculated to 0 The location, therefore, is 10 + 1
sizeof(*ss)  The results of  1  = = =" *ss Is the first character 
char ss[100] = "0123456789";
sizeof(ss)  As a result, 100  = = =" ss Represents the size in memory  100 x 1
strlen(ss)  As a result, 10  = = =" strlen It's an internal implementation of a function that is computed by a loop 0 So far before 
int ss[100] = "0123456789";
sizeof(ss)  The results of  400  = = =" ss Represents the size in memory  100 x 4
strlen(ss)  error   = = =" strlen The parameter can only be char*  And must be ''0'' At the end of the 
char q[]="abc";
char p[]="an";
sizeof(q),sizeof(p),strlen(q),strlen(p);
 As a result,  4 3 3 2  

 
Iv. Differences between sizeof and strlen:
 
1. Sizeof is the operator, strlen is the function. When an array is passed to a function as an argument, it passes a pointer instead of an array, and it passes the first address of the array, such as: fun(char [8]), fun(char []), which is equivalent to fun(char *). Therefore, the array does not degrade the argument of sizeof, so it will degenerate into a pointer if passed to strlen.

2. Sizeof can be used as an argument, strlen can only use char* as an argument, and must end with ''\0'. Sizeof can also use functions as arguments, such as short f();   Printf (" % d \ n ", sizeof (f ())); The output is sizeof(short), which is 2.
 
3. Most compilers calculate sizeof as the length of a type or variable at compile time. That's why sizeof(x) can be used to define array dimensions.


char str[20]="0123456789";
int a=strlen(str); //a=10;
int b=sizeof(str); // while b=20;


When applied to a structure type or variable, sizeof returns the actual size. When applied to a static space array, sizeof returns the sizeof the entire array. The sizeof operator cannot return the sizeof a dynamically dispatched array or an external array.


Related articles: