Sizeof and strlen explained

Article directory

  • sizeof operator
  • Example 2
  • Example 3
  • strlen library function

sizeof operator

1.sizeof operator is used to calculate the memory size of variables, the unit is bytes
2. You can also directly calculate the data type to find the number of bytes.

  • We use examples to learn sizeof
  • First we need to understand two things
    1.sizeof (array name), at this time the array name represents the entire array, and the number of bytes obtained is the size of the entire array.
    2. & amp; array name, indicating the size of the entire array
  • Other occurrences of the array name are treated as the address of the first element of the array.
  • What is written in the brackets of sizeof() is calculated. Pay attention to whether it is a variable, an element, or an address.
  • Here the address also needs to pay attention to the array address or the first element address.
  • What needs to be analyzed and stored in a two-dimensional array is the entire arraythe address of the first element of the array
  • The address of a certain row in the arrayA certain element in the arrayThe address of a certain row in the array
#inculde <stdio.h>


int main()
{<!-- -->
 int a = 10;
  Calculate variables
 printf("%d\
", sizeof(a));
 
You don’t need to add parentheses when calculating the memory size of a variable.
 printf("%d\
", sizeof a);

 The storage byte space can be obtained for the data type
 printf("%d\
", sizeof(int));

 
 return 0;
 }

Example 2

char arr[] = {<!-- -->'a','b','c','d','e','f'};
//Pay attention to the sizeof array name, where the array name represents the entire array
//char data type occupies one byte, there are 6 characters in the array, so the result is 6 bytes
printf("%d\
", sizeof(arr))

The arr in the brackets of sizeof() is not a single array name, so here it is treated as the first address of the array
arr + 0==arr is still the first address, note that the address memory occupies 8 bytes in X64, and 4 bytes in x86
printf("%d\
", sizeof(arr + 0));

Here the array does not appear alone, so it is the first address, which is the address of 'a',
The dereference operation accesses the 'a' element, so the result of the calculation is 1 byte
printf("%d\
", sizeof(*arr));

Here are the elements, arr[1]==*(arr + 1)
Add 1 to the arr address to access the second element address, * points to the second element
The result is 1 byte because it is one element
printf("%d\
", sizeof(arr[1]));

 & amp;The array name represents the entire array address.
The address occupies 4/8 bytes
printf("%d\
", sizeof( & amp;arr));

 & amp;The array name represents the entire element address
Note: & amp;arr is an entire array address, so it should be stored with an array pointer
And its data type is int (*)[6], adding 1 after it depends on whether it is a value or an address
Adding 1 to the value is adding 1. If it is an address, it depends on the data type. The entire array type here
It is int (*)[6] + 1, so an offset of 6 bytes at a time is the entire array, because char
An element of the type array occupies 1 byte, so an array is offset
Address occupies byte 4/8
printf("%d\
", sizeof( & amp;arr + 1));
arr[0] is the address of the first element as the first address, plus 1, offset by 1 byte
Because the data type of & amp;arr[0] is char*
The result is 4/8
printf("%d\
", sizeof( & amp;arr[0] + 1));


Example 3

Note that the string here will have a \0 by default
Remember that the pointer stores the address, don't be fooled by the surface
The character pointer stores the character string (first) address of the first character
In fact, strings can also be regarded as an array, and they are also continuous.
char *p = "abcdef";
Here p is the address, so the result is 4/8 bytes
printf("%d\
", sizeof(p));
The address p stores the address of a + 1, offset by one byte, p + 1 is the address pointing to b
The result is 4/8
printf("%d\
", sizeof(p + 1));
p stores the address of a, *indirect addressing finds a, and calculates the memory size of a
The result is 1 byte
printf("%d\
", sizeof(*p));
p[0]==*(p + 0)==*p
As mentioned above, a string can be regarded as an array, and p is the address to find the element through the subscript, which is a
The result is 1
printf("%d\
", sizeof(p[0]));
p stores the address of a, & amp;p indicates the address of p, and the pointer also has an address
Its type is char *
The result is 4/8 bytes
printf("%d\
", sizeof( & amp;p));
 & amp;p, the address of p plus 1 depends on the data type char* offset by one byte
The result is 4/8 bytes
printf("%d\
", sizeof( & amp;p + 1));
p[0] finds a & p[0] takes out the address of a + 1, offset by one byte
The result is 4/8 bytes
printf("%d\
", sizeof( & amp;p[0] + 1));



Note: The byte space occupied by the address has nothing to do with the data type, but with the platform environment

strlen library function

strlen is a C language library function, included in the <string.h> header file
The function is to find the length (number) of a string
The principle of strlen():
Through the given address, it has been traversing the number of characters until it meets \0 before stopping
Number of characters: the number between the address and \0
Function prototype:
size_t strlen ( const char * str );
Its parameter is a pointer. If it is a variable, an error will be reported, and an illegal access will occur.


Example 1

 char arr[] = {<!-- -->'a','b','c','d','e','f'};
   Here arr is the name of the array, representing the address of the first element,
   Starting from the arr address and looking for \0;
   Characters are used to store here, and \0 is not stored.
   The result is a random value
printf("%d\
", strlen(arr));
This is the same as the previous one arr + 0==arr
printf("%d\
", strlen(arr + 0));
If arr dereferences the space pointing to the stored address, an error will be reported for illegal access
printf("%d\
", strlen(*arr));
arr[1]==*(arr + 1) is also an element, and an illegal error is reported.
printf("%d\
", strlen(arr[1]));
& amp; arr represents the entire array address, & amp; arr starting point is also the first address,
So it also counts characters from the first address to \0, the characters stored in the array here
There is no \0, so it will look for \0 backwards, and the result is a random value
printf("%d\
", strlen( & amp;arr));
& amp; arr, the data type is char (*) [6] + 1 offset 6 bytes at a time
printf("%d\
", strlen( & amp;arr + 1));
printf("%d\
", strlen( & amp;arr[0] + 1));
  • Example 2
The string stored in the pointer is the address of the first character
Strings are stored in \0 by default
char *p = "abcdef";
Strings can be treated as arrays
Here, p is stored in the address of a, and the statistics from the address of a know that \0 is found
So it is 6 bytes
printf("%d\
", strlen(p));
p is stored in address a, the type is char*, + 1 offset by 1 byte points to address b starting from address b
Statistics know \0 ends
The result is 5 bytes
printf("%d\
", strlen(p + 1));

p is the address of a, dereferencing is a, so it is an illegal access
printf("%d\
", strlen(*p));
p[0]==*(p+0)==*p
It is element a, strlen will be converted into the ascll code of address a, which is 97 as the address, so it is illegal to access
printf("%d\
", strlen(p[0]));
 & amp;p takes the address of the pointer, starting from the p address and looking for \0,
So it is a random value
printf("%d\
", strlen( & amp;p));
 & amp;p + 1, its data type is char** + 1, offset 1 byte to find \0,
So it is a random value
printf("%d\
", strlen( & amp;p + 1));
p[0] is the character a element & the address taken is the address of a + 1, offset by 1 byte, pointing to b
So it is 5 bytes
printf("%d\
", strlen( & amp;p[0] + 1));

Illegal access needs to be commented out, otherwise the following values cannot be output.

Use of two-dimensional array sizeof
int a[3][4] = {<!-- --> 0 };
//sizeof (array name) represents the entire array, so the memory size of the entire array is calculated.
//The result is 3*4*4==48 bytes
printf("%d\
", sizeof(a));
//a[0][0]==*(*(a + 0) + 0)
//It calculates the memory size of an element
//The result is 4 bytes because the int type occupies 4 bytes
printf("%d\
", sizeof(a[0][0]));
//a[0] is the array name, and the sizeof() array name exists alone
//a[0]==*(a + 0), this represents the first row of arrays, so the memory size of a row is calculated
//The result is 16
printf("%d\
", sizeof(a[0]));
//This is the array name + 1, so this cannot represent the address of a row
//This is the first address of the first element + 1, offset by 4 bytes, pointing to the next address
// so the result is 4/8 bytes
printf("%d\
", sizeof(a[0] + 1));
//a[0] + 1 represents the next address and dereferences the element representing the address, so it is an element
//The result is 4 bytes
printf("%d\
", sizeof(*(a[0] + 1)));
//a is the name of the array, but it does not exist independently and cannot represent the entire array.
//So it is the first address of the first element of the array, which represents the address of this row + 1
//Point to the next line a[1]
//The result is 4/8 bytes
printf("%d\
", sizeof(a + 1));
//The address representing this row + 1 points to the next row a[1]
//So the calculation is the memory size of a row of elements
//The result is 16 bytes
printf("%d\
", sizeof(*(a + 1)));
//a[0] represents the address of the first element of the first row
// & amp; address is the first address + 1, pointing to the address of a[1]
//The result is 4/8 bytes
printf("%d\
", sizeof( & amp;a[0] + 1));
//We said & amp; array name represents the whole element
//Here & amp;a[0] represents the first row address + 1
//Represents the dereferencing of the second row address as the second row element
//Calculate the memory size of the second row element
//16 bytes
printf("%d\
", sizeof(*( & amp;a[0] + 1)));
//a array name does not exist alone, it represents the address of the first line,
//Dereference represents a row of elements
//The result is 16 bytes
printf("%d\
", sizeof(*a));
//The essence of the sizeof operator is to calculate the size of memory occupied by a type or variable at compile time.
//It does not really calculate the size of the object, but according to the compiler's data type regulations,
// Determine its size. Therefore, the sizeof operator is evaluated at compile time.
//a[3] appears alone to represent the fourth row of arrays, but sizeof will not actually calculate,
//But according to the compiler's regulations on data types, then it is the same as the result of the first line, the second line, and the third line
//The result is 16 bytes;
printf("%d\
", sizeof(a[3]));
return 0;
}