The result of the sizeof function:
- Variable: The number of bytes that the variable occupies.
int 0 ;p rintf ("%d\n"sizeof(i)); // 4
- Array: The number of bytes that the array occupies.
intArr_int1[] = {1,2,3,4,5};intarr_int2[Ten] = {1,2,3,4,5};p rintf ("size_arr1=%d\n",sizeof(Arr_int1));//5*4=20printf"size_arr2=%d\n",sizeof(Arr_int2));//10*4=40
- String: Actually, it is an array of characters that have been added ' \ s '. The result is a string character length of +1.
Char " Str " ;p rintf ("size_str=%d\n",sizeof(str)); // 3+1=4
- Pointer: fixed Length: 4 (32-bit address environment).
- Special Description: When an array is an entry parameter to a function, the logarithm of the group sizeof in the function is fixed to 4: Because the passed-in parameter is a pointer.
int get_size (int arr[]) { return sizeof Span style= "COLOR: #000000" (arr);} int main () { int arr_int[10 ] = {1 , 2 , 3 , 4 , 5 }; printf ( " size_fun_arr=%d\n ", Get_size (Arr_int)); // 4 }
- Structural body
- Structure with variables only:
- The result is an integer multiple of the maximum number of bytes for the widest variable: [4 1 xx x]
struct Test { int i; Char ch;} test_t;printf ("size_test=%d\n"sizeof(test_t)); // 8
- several variables with a smaller width can be populated within a width range: [4 2 1 1]
typedef struct Test { int I ; short S; char ch1; char CH2;} test_t;printf ( " size_test=%d\n Span style= "COLOR: #800000" > ", sizeof (test_t)); // 8
- address alignment: The offset of a struct member must be an integer multiple of its own width: [4 1 x 2 1 x x x]
typedef struct Test { int
I; char ch1; short S; char CH2;} test_t;printf ( " size_test=%d\n Span style= "COLOR: #800000" > ", sizeof (test_t)); // 12
- struct with array: Contains the width of the entire array. The array width is detailed above. [4*10 2 1 1]
struct Test { int i[]; Short s; Char ch1; Char CH2;} test_t;printf ("size_test=%d\n"sizeof(test_t)); // -
- Structure of nested structure body
- Contains the width of the entire internal structure (that is, the entire expanded internal structure): [4 4 4]
struct son { int name; int birthday; } son_t; struct Father { son_t s1; int Wife;} father_t;printf ("size_struct=%d\n",sizeof(father_t)); // A
- Address alignment: The internal structure of the expandedoffset for first member, it must be unfolded.the widest variable in the internal structureInteger multiples of the occupied bytes: [2x x2 x x 4 4 4]
struct Son { short age ; int name; int birthday; } son_t; struct Father { short age ; son_t S1; int Wife;} father_t;printf ("size_struct=%d\n",sizeof(father_t)); // -
A summary of the sizeof function in C language