[Pointer and Array Pen Test Questions (1)] Detailed Explanation of Pointer and Array Pen Test Questions

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

To understand the basic concepts before calculating:

Understanding of the array name
The array name is the address of the first element of the array .
There are two exceptions
1. sizeof (array name), where the array name represents the entire array, and the calculation is the size of the entire array in bytes.
2.&Array name, where the array name represents the entire array, and the address of the entire array is taken out


integer array

#include<stdio.h>

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

a is the array name, the array name is placed inside sizeof alone, and the calculation is the size of the entire array, 4*4=16, size(a) is 16 bytes

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

The array name a is the address of the first element of the array, a+0 is also the address of the first element of the array, and the address is 4/8 bytes

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

*a=a[0], the array name a is the address of the first element of the array, *a is the first element, the size is not the address, the size of the integer is 4 bytes

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

The array name a is the address of the first element of the array, a+1 is the address of the second element of the array, and the address is 4/8 bytes

printf("%d\n", sizeof(a[1]));

a[1] = (a + 1), a is the address of the first element of the array, a+1 is the address of the second element of the array, (a+1) is the second element, which is to find the size, and the size of the integer array is 4 bytes.

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

&a takes out the address of the entire array, the address is 4/8 bytes

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

sizeof(*&a)=sizeof(a), where a represents the entire array, and the calculation is the size of the entire array, in bytes. 4*4=16

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

&a means the address of the entire array, &a+1 means skip the entire element, still the address, the address is 4/8 bytes

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

&a[0] is the address of the first element of the array, 4/8 bytes

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

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

	return 0;

}

In the x64 environment, it is 8 bytes, and in the x86 environment, it is 4 bytes.
Usually, the x86 environment is used.
insert image description here

insert image description here

character array

The first set of questions

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

arr is placed inside sizeof alone, and the calculation is the size of the entire array, an array of char type, the unit is one byte, 1*6=6

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

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

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

arr represents the address of the first element of the array, *arr represents the first element, note that the calculation is not the address, but the size, the size of the first element is 1 byte

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

arr[1]=*(arr+1), arr[1] represents the second element, note that the calculation is not the address, but the size, the size of the second element is 1 byte

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

&arr is the address of the entire array, the address is 4/8 bytes

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

&arr is the address of the entire array, &arr+1 means skip the entire array, still means the address, the address is 4/8 bytes

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

&arr[0] is the address of the first element, &arr[0]+1 is the address of the second element, the address is 4/8 bytes

	return 0;
}

insert image description here


The second set of questions

strlen is a library function.
Its function is to find the length of the string. The count is the number of characters before \0 in the string.
If there is no \0, it will always look backward.

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

Because there is no \0 in the character array arr, when finding the length of the string, the length of the string will be counted backwards, and the result is a random value
printf("%d\n", strlen(arr + 0));
//arr is the address of the first element, arr+0 is still the address of the first element, same as the first one, or a random value

printf("%d\n", strlen(*arr));
//错误代码

arr is the address of the first element of the array, *arr is the first element of the array, which is 'a', the ASCII code value of 'a' is 97, the
strlen function parameter needs to pass the address, that is, pass 97 as the address to strlen, and strlen starts from 97 This address counts the length of the string later, which is an illegal memory access.

//错误代码
printf("%d\n", strlen(arr[1]));

arr[1] is the second element of the array, 'b', the ASCII code value of 'b' is 98, so it is also an error code

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

&arr is the address of the array, the address of the array is the same as the address of the first element, so after passing it to the strlen function, the length of the string is still counted backwards from the first element of the array, and the random value

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

&arr is the address of the first element, &arr+1 skips the entire array, counts the length of the string from the end of the array, and the result is also a random value

printf("%d\n", strlen(&arr[0] + 1));

&arr[0] + 1 is the address of the second element, strlen counts the length of the string backward from the address of the second element, and the result is also a random value

	return 0;
}

insert image description here


The third set of questions

int main()
{
    
    
	char arr[] = "abcdef";//里面实际放有[a b c d e f \0]
printf("%d\n", sizeof(arr));

arr is placed in sizeof alone, the calculation is the size of the entire array, the unit is byte, the
char type array, the unit is one byte, 1*7=7, the result is 7 bytes

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

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

printf("%d\n", sizeof(*arr));//*arr=*(arr+0)=arr[0]

arr represents the address of the first element, *arr represents the first element, the array type is char type, and the unit is one byte, so the size of the first element is one byte

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

arrp[1] represents the second element, the size is one byte

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

&arr represents the entire array address, the address is 4, 8 bytes

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

&arr means the entire array address, &arr+1 means skip the entire array, still the address, the address is 4/8 bytes

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

&arr[0] indicates the address of the first element, &arr[0]+1 indicates the address of the second element, the address is 4/8 bytes

		return 0;
}

insert image description here

Summarize

The above is the pointer and array written test questions and detailed explanations of the first part.

Guess you like

Origin blog.csdn.net/2301_76496134/article/details/131879960