Different array parameters have different meanings in sizeof and strlen

Table of Contents

Basic understanding of array parameter passing

Basic understanding of sizeof and strlen

sizeof operator:

strlen function:

Notice:

sizeof and int type array

Code:

Code analysis:

sizeof and character array

Code 1, char arr[]={0}:

Code analysis:

Code 2, char arr[] = ” “:

Code analysis:

strlen and character array

Code 1, char arr[]={0}:

Code analysis:

Code 2, char arr[] = ” “:

Code analysis:

sizeof and string pointer

Code:

?Edit code analysis:

strlen and string pointers

Code:

Code analysis:

sizeof and two-dimensional array

Code:

Code analysis:


Basic understanding of array parameter passing

The array name is the address. Generally speaking, the array name is the address of the first element of the array.

But there are two exceptions

1. sizeof (array name). The array name here represents the entire array. The calculation is the size of the entire array. The unit is bytes.

2. & amp; array name. The array name here represents the entire array, and what is taken out is the address of the entire array.

In addition, all array names are the address of the first element of the array.

Basic understanding of sizeof and strlen

strlen and sizeof are two commonly used string manipulation functions in C language

sizeof operator:

Used to calculate the number of bytes occupied by a data type or variable, and can be used to calculate the size of an array

char str[] = "Hello, world!";
size_t strSize = sizeof(str); //Calculate the total number of bytes occupied by the str array, the result is 14 (including the '\0' at the end of the string)
int arr[] = {1, 2, 3, 4};
size_t arrSize = sizeof(arr); //Calculate the total number of bytes occupied by the arr array, the result is 16 (4 int types, each occupying 4 bytes)

strlen function:

Used to calculate the length of a string, the function prototype is

size_t strlen(const char *str);

The parameter str is a pointer to the string whose length needs to be calculated.

The function return type is size_t, which is an unsigned integer, for example

char str[] = "Hello, world!";
size_t len = strlen(str); //Calculate the length of str, the result is 13

Note:

The result unit returned by sizeof is bytes, not the number of string or array elements.

At the same time, the sizeof operator can be used for calculations of any data type, while the strlen function can only be used to calculate the length of strings ending with ‘\0’

sizeof and int type array

Let’s take a look at a code to analyze what the output results of the following codes are and how they are obtained.

code:

int main()
{
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));
return 0;
}

Code analysis:

sizeof(a)

The array name is placed separately, so the size of the entire array is calculated.

sizeof(a + 0)

It is not placed inside sizeof alone, and there is no & amp;
Therefore, it represents the address of the first element of the array, a + 0 is still the address of the first element,

Calculate the size of the address. The address is 4/8 bytes.

8 bytes in x64 environment, 4 bytes in x86 environment, the blogger environment is x86

sizeof(*a)

a is not placed inside sizeof alone, and there is no & amp;, so it is the address of the first element of the array

*a is the first element, int type size is 4Byte, *a == *(a + 0)

sizeof(a + 1)

a is not placed inside sizeof alone, and there is no & amp;, so it is the address of the first element of the array

a + 1 is the address of the second element a + 1== & a[1], the address is 4/8 bytes

sizeof(a[1])

a[1] is the second element of the array, and what is calculated here is the size of the second element.

sizeof( & amp;a)

& amp;a is the address to take out the array. The address of the array is also an address. The address is 4/8 Byte.
The essential difference between the address of the array and the address of the first element of the array is the difference in type, not the size.
a — int* int* p = a;
& amp;a — int(*)[4] int (*p)[4] = & amp;a;

sizeof(* & amp;a)

Dereferencing an array pointer to access the size of an array calculates the size of the entire array.
sizeof(* & amp;a) == sizeof(a)

sizeof( & amp;a + 1)

& amp;a The address of the array, & amp;a + 1 is still an address, and the address is 4/8 Byte

sizeof( & amp;a[0])

&a[0] is the address of the first element, and the size of the address is calculated. The address is 4/8 Byte

sizeof( & amp;a[0] + 1)

& amp;a[0] is the address of the first element, & amp;a[0] + 1 is the address of the second element, and the address is 4/8 Byte

sizeof and character array

Code 1, char arr[]={0}:

int main()
{
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));
return 0;
}

Code analysis:

sizeof(arr)

The array name is placed alone inside sizeof. The size of the entire character array is calculated. One character occupies one byte.

sizeof(arr + 0)

The array name is not placed inside sizeof alone, it represents the address of the first element.

arr + 0 still represents the address of the first element, which is 4/8 bytes.

sizeof(*arr)

It is not placed inside sizeof alone, it represents the address of the first element.

*arr represents the first element, one character occupies 1 byte

sizeof(arr[1])

It is not placed inside sizeof alone. arr[1] represents the second element, and one character occupies one byte.

sizeof( & amp;arr)

& amp;arr, obtain the address of the entire character array, the address is 4/8 bytes

sizeof( & amp;arr + 1)

It is not placed inside sizeof alone, but the address of the first element is obtained.

+ 1 is the address of the second element, which is 4/8 bytes.

sizeof( & amp;arr[0] + 1)

arr[0] represents the first element, & amp;arr[0] is the address of the first element,

+ 1 is the address of the second element, which is 4/8 bytes.

Code 2, char arr[] = ” “:

int main()
{
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));
return 0;
}

Code analysis:

sizeof(arr)

The array name is placed alone inside sizeof to calculate the size of the entire array.

char arr[] = “abcdef”;//a b c d e f \0

There is a ‘\0’ hidden behind the array, so it is 7 bytes

The most important difference is this. The other contents are consistent with the code 1. What needs to be noted is that there is ‘\0’ hidden behind the string.

strlen and character array

strlen finds the length of a string
//The count is the number of characters appearing before \0 in the string

Code 1, char arr[]={0}:

int main()
{
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));
return 0;
}

Only two results appear, and the output seems wrong.

The two results are because the third code is transmitted incorrectly. Please see the code analysis below for details.

Code analysis:

strlen(arr)

arr is the address of the first element. The reason why the output is 19 is because

char arr[] = { ‘a’,’b’,’c’,’d’,’e’,’f’ } does not end with ‘\0’

We mentioned above that the strlen function can only be used to calculate the length of strings ending with ‘\0’

I don’t know when I will encounter ‘\0’ later, so it is a random value.

strlen(arr + 0)

arr is the address of the first element, arr + 0 is still the address of the first element

It will stop when it encounters ‘\0’, so it is a random value.

strlen(*arr)

arr is the address of the first element, *arr is the first element ‘a’ –>97
From the perspective of strlen, it is believed that the ‘a’ –> 97 passed in as the parameter is the address.
97 is accessed directly as an address, which is illegal access.

strlen(arr[1])

arr[1] is the second element of the character array, which is ‘b’

‘b’-98, illegal access

strlen( & amp;arr))

& amp;arr As a parameter, the address of the entire string array is passed to the strlen() function to calculate the length of the string array, so it is still a random value

strlen( & amp;arr + 1)

Get the address of the entire string array, + 1 skips the entire string

Points to the unknown memory area behind the arr array. It is not a valid string address, so the strlen function will have undefined behavior. The result is unpredictable, so it is also a random value.

strlen( & amp;arr[0] + 1)

&arr[0] represents the first address of the array, + 1 represents calculation starting from the address of the second element

There is no ‘\0’, so it is a random value

Code 2, char arr[] = ” “:

int main()
{
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));
return 0;
}

Code analysis:

strlen(arr)

arr represents the address of the first element, calculated from the first element until the end of ‘\0’

In this string, there is ‘\0’ hidden, so the answer is 6

strlen(arr + 0)

arr is the address of the first element, arr + 0 is still the address of the first element

Count from the first element until the end of ‘\0’, answer 6

strlen(*arr)

arr is the address of the first element, *arr is the first element ‘a’ –>97
From the perspective of strlen, it is believed that the ‘a’ –> 97 passed in as the parameter is the address.
97 is accessed directly as an address, which is illegal access.

strlen(arr[1])

arr[1] is the second element of the character array, which is ‘b’

‘b’-98, illegal access

strlen( & amp;arr)

& amp;arr As a parameter, the address of the entire string array is passed to the strlen() function,

Calculate the length of the string array, so answer 6

strlen( & amp;arr + 1)

Get the address of the entire string array, + 1 skips the entire string

Points to the unknown memory area behind the arr array. It is not a valid string address, so the strlen function will have undefined behavior. The result is unpredictable, so it is a random value.

strlen( & amp;arr[0] + 1)

&arr[0] represents the first address of the array, + 1 represents calculation starting from the address of the second element

Until the end of ‘\0’, the answer is 5

sizeof and string pointer

Code:

int main()
{
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));
return 0;
}

Code analysis:

sizeof(p)

p points to a string constant “abcdef”, and the size of the pointer variable p is calculated.

Pointers are equivalent to addresses

The size of pointer p is 4 bytes in 32-bit systems and 8 bytes in 64-bit systems

sizeof(p + 1)

p is the address of the first element of a character array "abcdef",

Adding 1 points to the second element of the array, which is the address of the character 'b'

But it is still an address, which is 4/8 bytes.

sizeof(*p)

p is the address of the first element of a character array "abcdef", *p points to the first element ‘a’

One character occupies one byte

sizeof(p[0])

p points to a string constant “abcdef”

When p[0] is accessed, it points to the first character ‘a’ of the string, which is equivalent to *(p + 0)

sizeof( & amp;p)

p stores the address of the first element of “abcdef”, & amp;p obtains the address of p

It’s still an address, it’s 4/8 bytes.

sizeof( & amp;p + 1)

& amp;p + 1 points to the memory address where pointer p is located and then moves backward by one byte occupied by the pointer.

That is, it points to the address after p, and the value of &p + 1 is uncertain.

But it is still an address, which is 4/8 bytes.

sizeof( & amp;p[0] + 1)

& amp;p[0] + 1 points to the memory address of the second character ‘b’ of the string “abcdef”.

Because & amp;p[0] represents the memory address of the first element of the character array

Adding 1 is equivalent to the memory address pointing to the second element of the character array.

But it is still an address, which is 4/8 bytes.

strlen and string pointer

Code:

int main()
{
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));
return 0;
}

Code analysis:

strlen(p)

p points to a string constant “abcdef”, and p stores the address of the first element.

‘\0’ is hidden behind, so the answer is 6

strlen(p + 1)

p stores the address of the first element. Adding 1 points to the address of the second element, which is b’.

The answer is 5

strlen(*p)

*p points to a. When calling the strlen() function, a pointer of type char* should be passed in.

What is passed here is a character, so there is a compilation error.

strlen(p[0])

When p[0] is accessed, it points to the first character ‘a’ of the string, which is equivalent to *(p + 0)

Parameter passing error

strlen( & amp;p)

& amp;p is passed to the strlen function, which actually passes the address of p.

Search backward from the address of p until it ends with ‘\0’, so it is a random value.

strlen( & amp;p + 1)
Get the address of p, add 1 to get the address after p

Search from the address behind p until you encounter ‘\0’ and it ends, which is a random value.

strlen( & amp;p[0] + 1)

& amp;p[0] + 1 points to the memory address of the second character ‘b’ of the string “abcdef”.

Because & amp;p[0] represents the memory address of the first element of the character array

Adding 1 is equivalent to the memory address pointing to the second element of the character array.

The answer is 5

sizeof and two-dimensional array

Code:

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

Code analysis:

sizeof(a)

Put the array name alone and calculate the entire array size

sizeof(a[0][0])

Calculate the size of the first element

sizeof(a[0])

A two-dimensional array is actually an array of arrays, which is an array of one-dimensional arrays.
a[0] is the array name of the one-dimensional array in the first row. The array name is placed separately inside sizeof.
What is calculated is the size of the entire first row of the one-dimensional array.

sizeof(a[0] + 1)

a[0] is not placed alone inside sizeof, and there is no & amp;
So it represents the address of the first element of the one-dimensional array in the first row, which is the address of the first element in the first row.
a[0]< -- > & amp;a[0][0] a[0] + 1 — > & amp;a[0][1]

sizeof(*(a[0] + 1))

a[0] + 1 is the address of the second element in the first row, *(a[0] + 1) the second element in the first row

sizeof(a + 1)

a, as the array name of a two-dimensional array, is not placed inside sizeof alone, and there is no & amp;

a represents the address of the first element of the array, which is the address of the first row.

a + 1 is the address of the second line, which is 4/8 bytes.

sizeof(*(a + 1))

a + 1 is the address of the second line, *(a + 1) is the second line, and the size of the second line is calculated – 16 bytes

Another angle — *(a + 1) –》a[1],

a[1] is the array name of the one-dimensional array in the second line. The array name is placed inside sizeof alone.

sizeof( & amp;a[0] + 1)

a[0] is the address of the first row, &a[0] takes out the address of the first row, and the type is int(*)[4]
& amp;a[0] + 1 is the address of the second line, which is 4/8 bytes.

sizeof(*( & amp;a[0] + 1))

a[0] is the address of the first row, &a[0] takes out the address of the first row, and the type is int(*)[4]
& amp;a[0] + 1 is the address of the second line

After dereferencing, what you get is the second line — 16 bytes

sizeof(*a)

a represents the address of the first element of the array, that is, the address of the first row

*a is a dereference to the address of the first line, which is equivalent to the array name of the first line — 16 bytes

sizeof(a[3])

It will not cross the boundary. sizeof is calculated based on the type and will not actually access – 16 bytes.

Two properties of expressions: value properties and class properties

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