What is the difference between sizeof and strlen, and what is the difference between sizeof(a) and sizeof(a+0)? sizeof(*a), sizeof(a+1), sizeof(a[1])… | Written test question analysis | Super complete

After experiencing array, pointer, pointer array, array pointer…, we really It is difficult to clearly understand what the relationship between them is. Below, the editor will use a written test question to clearly explain sizeof and Different types of calculation scenarios in >strlenand how to use them.

1. Essential knowledge points

We first need to master the different ways of using sizeof and strlen:

(1) How to use sizeof()

sizeof is actually just an operator. We should note that it is not a function. It is similar to the common +, =, -… operators, and sizeof is a unary operator. sizeof actually obtains the storage space occupied by data in memory, counted in bytes.

sizeof() is used for data types

When we first learned C language, we already knew that sizeof can calculate the storage space occupied by various data types. For example, recall the following code:

#include <stdio.h>
int main()
{
printf("%d\
", sizeof(int));
printf("%d\
", sizeof(char));
printf("%d\
", sizeof(short));
printf("%d\
", sizeof(long));
printf("%d\
", sizeof(long long));
printf("%d\
", sizeof(double));
printf("%d\
", sizeof(float));
printf("%d\
", sizeof(long double));

return 0;
}

The result of running is (different calculators have different results):

4
1
2
4
8
8
4
8

sizeof() for variables

sizeof is used for variables to calculate the size of the entire array, in bytes.

We can look at the following example:

#include <stdio.h>
int main()
{
int a = 5;
int b = 10;
int arr[] = { 1,2,3,4 };

    printf("%d\
", sizeof(a));
printf("%d\
", sizeof(b));
printf("%d\
", sizeof(arr));
\t
return 0;
}

The result of running is:

4
4
16

(2) How to use strlen()

strlen() is a function that is the number of characters in string. sizeof () can be used for any type of data, while strlen () can only be used for strings that terminate with the null character ‘\0’.

size_t strlen ( const char * str ) ;

Header file ofstrlen function:

#include <string.h>

Here we note that we need to pass a string address into strlen, and the strlen function calculates the number of characters through this address. For example, the following code:

int a = 0;
pritnf("%d\
", strlen(a));

This code is obviously wrong because it does not meet the following two points:

  • strlen is a method that counts the number of characters in string.
  • strlen needs to be passed into the address of a string.

And the correct way to use it:

#include <string.h>
#include 
int main()
{
char arr[] = {'a','b','c','d','e','f'};
char arr1[] = { 'a','b','c','d','e','f','\0'};

printf("%d\
", strlen( & amp;arr));
printf("%d\
", strlen( & amp;arr1));

return 0;
}

It should be noted that strlen calculates the number of characters before the string ‘\0’, and in the above code, the arr array does not have ‘\0’, so it is not known where ‘\0’ is in the memory.

The result of running is:

random value

6

(3) Meaning of array name

In addition to what we need to master above, it is more important for us to know the meaning of the array name; in the process of learning, we seem to often encounter the “first element address”, so how do we understand the array name? I have compiled a summary for you:

The array name is the first element address, but there are two exceptions:

  • sizeof (array name), calculates the size of the entire array;
  • &array name takes the address of the entire array.

2. sizeof() and strlen() written test questions

(1) One-dimensional array:

1 . int a[] = {1,2,3,4}

int a[] = {1,2,3,4};

printf("%d\
",sizeof(a));
printf("%d\
",sizeof(a + 0));
printf("%d\
",sizeof(*a));
printf("%d\
",sizeof(a + 1));
printf("%d\
",sizeof(a[1]));
printf("%d\
",sizeof( & amp;a));
printf("%d\
",sizeof(* & amp;a));
printf("%d\
",sizeof( & amp;a + 1));
printf("%d\
",sizeof( & amp;a[0]));
printf("%d\
",sizeof( & amp;a[0] + 1));

sizeof is used for variables to calculate the size of the entire array, in bytes.

Then let’s analyze it in turn:

  • sizeof(a)
int a[] = {1,2,3,4};//4 elements, each element is of type int (4 bytes)
printf("%d\
",sizeof(a));//16
                         //The array name a is placed alone inside sizeof. The array name represents the entire array, and the size of the entire array is calculated.
                         //The unit is bytes, 16 bytes

a[ ] This entire array occupies 16 bytes. Let’s look at this sizeof(a) , and here sizeof (array name) represents the entire array, and its calculation is the size of the entire array, in bytes.

operation result:

16

  • sizeof(a + 0)
int a[] = {1,2,3,4};
printf("%d\
",sizeof(a + 0));

In sizeof(a + 0) here, a is not placed inside sizeof alone, nor is the address taken, so the array name represents the first element address. In sizeof(a + 0), a + 0 is still the address of the first element, and the size of the address is 4/8 bytes.

  • sizeof(*a)
int a[] = {1,2,3,4};
printf("%d\
",sizeof(*a));

a is not placed inside sizeof alone, nor is the address taken, so the array name represents the first element address. *a in sizeof(*a) refers to the dereference pointing to the address of the first element, so it is the first element, and the size is 4 bytes. It can also be understood as:

*a == *(a + 0) == a [ 0 ]

So the running result is:

4

  • sizeof(a + 1)
int a[] = {1,2,3,4};
printf("%d\
",sizeof(a + 1));

In sizeof(a + 1) here, a is not placed inside sizeof alone, nor is the address taken, so the array name represents the address of the second element, which is the address, and the size of the address is 4 /8 bytes.

a + 1 == & amp;a[ 1 ]

So the running result is:

4,8

  • sizeof(a[1])
int a[] = {1,2,3,4};
printf("%d\
",sizeof(a[1]));

a[1] is the second element of the array. What is calculated here is the size of the second element. The unit is bytes, so it is 4 bytes.

So the running result is:

4

  • sizeof( & amp;a)
int a[] = {1,2,3,4};
printf("%d\
",sizeof( & amp;a));

& amp;a is the address to retrieve the array, but the address of the array is also an address, which is 4/8 addresses.

The essential difference between the address of the array and the address of the first element of the array:

It is atype difference,not a sizedifference.

So the running result is:

4, 8

  • sizeof(* & amp;a)
int a[] = {1,2,3,4};
printf("%d\
",sizeof(* & amp;a));

sizeof(* & a) is an array pointer type. It dereferences the array pointer type and accesses the size of the array, so it is 4 bytes.

sizeof(* & amp;a)— sizeof(a)

  • sizeof( & amp;a + 1)
int a[] = {1,2,3,4};
printf("%d\
",sizeof( & amp;a + 1));

In sizeof(&a + 1), &a is the address, &a + 1 is still the address, and the address is 4/8 bytes.

So the running result is:

4, 8

  • sizeof( & amp;a[0])
int a[] = {1,2,3,4};
printf("%d\
",sizeof( & amp;a[0]));

In sizeof(&a[0]), &a[0] is the address of the first element, and the size of the address is calculated as 4/8 bytes.

So the running result is:

4, 8

  • sizeof( & amp;a[0] + 1)
int a[] = {1,2,3,4};
printf("%d\
",sizeof( & amp;a[0] + 1));

In sizeof( & a[0] + 1), & a[0] is the address of the first element, so sizeof( & a[0] + 1) is the address of the second element, and the size is 4/ 8 bytes.

Address of the second elementExpression:

  • &a[ 1 ]
  • &a[ 0 ] + 1
  • a + 1

(2) Character array

1 . char arr[]={‘a’,’b’,’c’,’d’,’e’,’f’}

char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof(arr));
printf("%d\
", sizeof(arr + 0));
printf("%d\
", sizeof(*arr));
printf("%d\
", sizeof(arr[1]));
printf("%d\
", sizeof( & amp;arr));
printf("%d\
", sizeof( & amp;arr + 1));
printf("%d\
", sizeof( & amp;arr[0] + 1));
  • sizeof(arr)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof(arr));

The array name arr is placed alone inside sizeof. The size of the entire array is calculated in bytes, so the size is 6 bytes.

  • sizeof(arr + 0)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof(arr + 0));

The array name arr is not placed separately inside sizeof, so arr is the address of the first element, arr + 1 is the address of the second element, and the address is 4/8 bytes.

The size of the pointer type has nothing to do with the type. Regardless of the type of variable, the size is 4/8 bytes. Pointer variables are used to store addresses. How much space is needed to store the address? The size of the pointer variable is just a few bytes.

In a 32-bit environment, the address is 32 binary bits and requires 4 bytes, so the size of the pointer variable is 4 bytes.

In a 64-bit environment, the address is 64 binary bits and requires 8 bytes, so the size of the pointer variable is 8 bytes.

  • sizeof(*arr)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof(*arr));

The arr in *arr in sizeof(*arr) is not placed separately inside sizeof, and the address is not taken, so this arr is the address of the first element, so *arr is the first element. Size is 1 byte.

  • sizeof(arr[1])
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof(arr[1]));

sizeof(arr[1]) is the second element, which is 1 byte.

  • sizeof( & amp;arr)
char arr[] = {'a','b','c','d','e','f'}
printf("%d\
", sizeof( & amp;arr));

In sizeof(&arr), &arr is the address of the array, which is 4/8 bytes.

  • sizeof( & amp;arr + 1)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof( & amp;arr + 1));

In sizeof( & arr + 1), & arr + 1 is the address after skipping the array. The address is 4/8 bytes.

  • sizeof( & amp;arr[0] + 1)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", sizeof( & amp;arr[0] + 1));

In sizeof( & arr[0] + 1), & arr[0] + 1 is the address of the second element, and the address is 4/8 bytes.

char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen(arr));
printf("%d\
", strlen(arr + 0));
printf("%d\
", strlen(*arr));
printf("%d\
", strlen(arr[1]));
printf("%d\
", strlen( & amp;arr));
printf("%d\
", strlen( & amp;arr + 1));
printf("%d\
", strlen( & amp;arr[0] + 1));

strlen finds the length of a string, counting the number of characters that appear before ‘\0’ in the string.

  • strlen(arr)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen(arr));

Note that there is no ‘ \0 ‘ in this arr, so we don’t know when we will encounter ‘ \0 ‘. arr is the address of the first element, so the size is a random value.

  • strlen(arr + 0)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen(arr + 0));

Note that in arr + 0, arr is the address of the first element, so we don’t know when we will encounter ‘ \0 ‘. So the size is a random value.

  • strlen(*arr)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen(*arr));

arr is the address of the first element, *arr is the first element, which is the character ‘a’. In the ASCII code value, the character ‘a’ is 97. From the perspective of strlen, it is believed that ‘a’97 passed in is the address. Direct access is illegal access, so an error will be reported.

size_t strlen ( const char * str ) ;

So strlen needs to pass in an address

  • strlen(arr[1])
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen(arr[1]));

In the same way as above, arr[1] is the character ‘b’98. From the perspective of strlen, the ‘a’97 passed in is considered to be the address. Direct access is illegal access, so an error will be reported.

  • strlen( & amp;arr)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen( & amp;arr));

In strlen(& amp;arr), & amp;arr is the address of arr. Still because there is no ‘\0’ in this array, it is a random value.

  • strlen( & amp;arr + 1)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen( & amp;arr + 1));

In the same way as above, & amp;arr + 1 is also the same random value.

  • strlen( & amp;arr[0] + 1)
char arr[] = {'a','b','c','d','e','f'};
printf("%d\
", strlen( & amp;arr[0] + 1));

In the same way, & amp;arr[ 0 ] + 1 is also a random value.

2 . char arr[] = “abcdef”

char arr[] = "abcdef";
printf("%d\
", sizeof(arr));
printf("%d\
", sizeof(arr + 0));
printf("%d\
", sizeof(*arr));
printf("%d\
", sizeof(arr[1]));
printf("%d\
", sizeof( & amp;arr));
printf("%d\
", sizeof( & amp;arr + 1));
printf("%d\
", sizeof( & amp;arr[0] + 1));

We should note that by default the last character in the string contains ‘\0’.

  • sizeof(arr)
char arr[] = "abcdef";
printf("%d\
", sizeof(arr));

arr is placed alone in sizeof, which represents the size of the entire array, plus ‘\0’, which is actually 7 bytes.

  • sizeof(arr + 0)
char arr[] = "abcdef";
printf("%d\
", sizeof(arr + 0));

The array name arr is not placed separately inside sizeof, so arr is the address of the first element, arr + 1 is the address of the second element, and the address is 4/8 bytes.

  • sizeof(*arr)
char arr[] = "abcdef";
printf("%d\
", sizeof(*arr));

arr is not used alone in sizeof, so arr is the address of the first element, so *arr is the first element, so the size is 1 byte.

  • sizeof(arr[1])
char arr[] = "abcdef";
printf("%d\
", sizeof(arr[1]));

The second element, size is 1 byte.

  • sizeof( & amp;arr)
char arr[] = "abcdef";
printf("%d\
", sizeof( & amp;arr));

& amp;arr takes out the address of the array. The address of the array is also an address. The address is 4/8 bytes.

  • sizeof( & amp;arr + 1)
char arr[] = "abcdef";
printf("%d\
", sizeof( & amp;arr + 1));

&arr + 1 is also an address, which is 4/8 bytes.

  • sizeof( & amp;arr[0] + 1)
char arr[] = "abcdef";
printf("%d\
", sizeof( & amp;arr[0] + 1));

The address of the second element is 4/8 bytes.

char arr[] = "abcdef";
printf("%d\
", strlen(arr));
printf("%d\
", strlen(arr + 0));
printf("%d\
", strlen(*arr));
printf("%d\
", strlen(arr[1]));
printf("%d\
", strlen( & amp;arr));
printf("%d\
", strlen( & amp;arr + 1));
printf("%d\
", strlen( & amp;arr[0] + 1));
  • strlen(arr)
char arr[] = "abcdef";
printf("%d\
", strlen(arr));

arr does not take the address, nor is it placed inside sizeof alone, so the array name arr is the address of the first element, so the ‘\0’ at the end of the string is added, so the size is 6.

  • strlen(arr + 0)
char arr[] = "abcdef";
printf("%d\
", strlen(arr + 0));

rr does not take the address, nor is it placed inside sizeof alone, so the array name arr is the address of the first element, and arr + 0 is still plus the last ‘\0’ of the string, so the size is 6.

  • strlen(*arr)
char arr[] = "abcdef";
printf("%d\
", strlen(*arr));

arr is the address of the first element, and *arr is the first element. Because strlen needs to pass in an address, an error will be reported.

  • strlen(arr[1])
char arr[] = "abcdef";
printf("%d\
", strlen(arr[1]));

arr[1] is the second element, so an error will be reported.

  • strlen( & amp;arr)
char arr[] = "abcdef";
printf("%d\
", strlen( & amp;arr));

&arr address, take out the address of arr, count from front to back to know ‘ \0 ‘, so it is 6 bytes.

  • strlen( & amp;arr + 1)
char arr[] = "abcdef";
printf("%d\
", strlen( & amp;arr + 1));

Take the address of arr and skip the address of arr. We don’t know when we will encounter ‘\0’, so it is a random value.

  • strlen( & amp;arr[0] + 1)
char arr[] = "abcdef";
printf("%d\
", strlen( & amp;arr[0] + 1));

The address of arr[0] + 1 is the address of the second element, so the size is 5 characters.

3 . char *p = “abcdef”

char *p = "abcdef";
printf("%d\
", sizeof(p));
printf("%d\
", sizeof(p + 1));
printf("%d\
", sizeof(*p));
printf("%d\
", sizeof(p[0]));
printf("%d\
", sizeof( & amp;p));
printf("%d\
", sizeof( & amp;p + 1));
printf("%d\
", sizeof( & amp;p[0] + 1));

Note here that we do not simply put “abcdef” into *p. There must be a constant string stored there. Assume that the starting address of this block is 0x0012ff40, and there is also a piece of memory p 0x0012ff40 is stored, which points to “abcdef”.

  • sizeof(p)
char *p = "abcdef";
printf("%d\
", sizeof(p));

What is calculated is the size of the pointer variable, which is equivalent to the address, which is 4/8.

  • sizeof(p + 1)
char *p = "abcdef";
printf("%d\
", sizeof(p + 1));

Suppose p is 0x0012ff40, so p + 1 points to 0x0012ff41. p + 1 is still an address, which is 4/8 bytes.

  • sizeof(*p)
char *p = "abcdef";
printf("%d\
", sizeof(*p));

*p is a pointer to char*, dereferencing to access a byte.

*p == ‘ a ‘

  • sizeof(p[0])
char *p = "abcdef";
printf("%d\
", sizeof(p[0]));

Still the character ‘a’, so 1 byte.

p[0] == *(p + 0)

  • sizeof( & amp;p)
char *p = "abcdef";
printf("%d\
", sizeof( & amp;p));

This is equivalent to the secondary pointer char**, and the address is 4/8 bytes.

  • sizeof( & amp;p + 1)
char *p = "abcdef";
printf("%d\
", sizeof( & amp;p + 1));

Take address + 1, it is still an address, which is 4/8 bytes.

  • sizeof( & amp;p[0] + 1)
char *p = "abcdef";
printf("%d\
", sizeof( & amp;p[0] + 1));

&p[0] is the first address, &p[0] + 1 is the address of the second element, so it is 5 bytes.

char *p = "abcdef";
printf("%d\
", strlen(p));
printf("%d\
", strlen(p + 1));
printf("%d\
", strlen(*p));
printf("%d\
", strlen(p[0]));
printf("%d\
", strlen( & amp;p));
printf("%d\
", strlen( & amp;p + 1));
printf("%d\
", strlen( & amp;p[0] + 1);
  • strlen(p)
char *p = "abcdef";
printf("%d\
", strlen(p));

The default string has ‘\0’, so is 6 bytes.

  • strlen(p + 1)
char *p = "abcdef";
printf("%d\
", strlen(p + 1));

Skip one byte, so 5 bytes.

  • strlen(*p)
char *p = "abcdef";
printf("%d\
", strlen(*p));

*p points to a character ‘a’, which is the same as before. strlen needs to pass in an address, so an error will be reported.

  • strlen(p[0])
char *p = "abcdef";
printf("%d\
", strlen(p[0]));

p[0] is equivalent to the first character ‘ \0 ‘, and an error will also be reported.

  • strlen( & amp;p)
char *p = "abcdef";
printf("%d\
", strlen( & amp;p));

&p takes the address. We don’t know when ‘\0’ will appear at this time, so it is a random value.

  • strlen( & amp;p + 1)
char *p = "abcdef";
printf("%d\
", strlen( & amp;p + 1));

In the same way as above, we don’t know when ‘\0’ will appear at this time, so it is a random value.

  • strlen( & amp;p[0] + 1)
char *p = "abcdef";
printf("%d\
", strlen( & amp;p[0] + 1);

&p[0]+1 is the second character ‘b’, so starting from character ‘b’ is 5 characters.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 52220 people are learning the system