Explain in detail the use of strlen and sizeof in arrays (1)

 Table of contents

I. Introduction

2. What are sizeof and strlen? What's the difference?

sizeof type title

Topic analysis:

Under 32-bit platform:​

 Under 64-bit platform:


I. Introduction

     We learned the use of string functions earlier, but the string functions are basically over, now let’s talk about

Different things, about the use of sizeof and strlen


2. What are sizeof and strlen? What's the difference?

1. sizeof is an operator and does not require a header file. The form is generally sizeof (variable or expression), and the calculation is

   The amount of space occupied. The unit is bytes, so he can calculate the size of any type .

2. We are quite familiar with strlen, strlen is a string function , to lead to the header file <string.h> , and it can only calculate

   The size of a character , in bytes, cannot be used to calculate the size of other types, and it will stop when '\0' is encountered .

3. In fact, they have nothing to do with each other, one is an operator and the other is a function. The part marked in red above is theirs

   The main difference.


First insert a little knowledge : the array name is the address of the first element , only & array name and sizeof (array name)   represent the entire array, other

All are the address of the first element, only these two forms, in which sizeof (array name) can not add anything , and can not be less, such as

sizeof(array name+0), then the array name represents the address of the first element at this time . The pointer is the address under the 32-bit/64-bit platform

4/8 bytes.


Carefully understand the above words, after understanding, then the following topic:

sizeof type title

#include<stdio.h>
int main()
{
	int arr[] = { 1, 3, 5, 7, 9};
	printf("%d\n", sizeof(arr));

	printf("%d\n", sizeof(arr+1));

	printf("%d\n", sizeof(*arr));

	printf("%d\n", sizeof(&arr));

	printf("%d\n", sizeof(&arr[0]));

    printf("%d\n", sizeof(&*arr));

	printf("%d\n", sizeof(*&arr));

	printf("%d\n", sizeof(&arr+1));

	printf("%d\n", sizeof(*arr+1));

	printf("%d\n", sizeof(arr+0));

	return 0;
}

Topic analysis:

The first sizeof(arr) calculates the size of the whole array so the result is 5*4= 20 bytes.                                 

 The second sizeof(arr+1) where arr is the address of the first element, and arr+1 represents the address of the second element. Since it is an address, it is 4 or 8 bytes. If you don’t understand it, you can look at the above episode knowledge.

The third sizeof(*arr), *arr means to get the first element, and the type of the first element is int, so the size is 4 bytes.

The fourth sizeof(&arr), &arr means to get the address of the entire array, since it is an address, it is 4 or 8 bytes.

The fifth sizeof(&arr[0]), &arr[0] indicates that the address of the first element has been obtained, so it is 4 or 8 bytes.

The sixth sizeof(&*arr), because & and * have the same priority, but the associativity is from right to left, so first *arr means to get the first element, and then &(*arr), means to get The address of the first element, so 4 or 8 bytes.

The seventh is sizeof(*&arr), first &arr means to get the address of the whole array, *(&arr), means to get all the elements of the whole array, because the type of the array is int, so it is 5*4= 20 byte.

The eighth is sizeof(&arr+1), &arr gets the address of the entire array, &arr+1 skips the entire array, and gets the address of the next block because sizeof is only calculated, not specifically executed, so there is no out-of-bounds access, since it is an address , is 4 or 8 bytes.

The ninth is sizeof(*arr+1), arr is the first element, *arr gets the first element, (*arr+1) so the first element is incremented by one, (that is, adding 1) because it is an int type, so it is 4 bytes.

The tenth is sizeof(arr+0), a single sizeof(arr) represents the entire array, but arr+0 represents the address of the first element, so it is 4 or 8 bytes.


The result of running under the vs compiler is as follows:

Under 32-bit platform:


 Under 64-bit platform:

 So here it is. ok, see you next time


Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/120333056#comments_27271315