当前位置:网站首页>Pointer for in-depth analysis (problem solution)
Pointer for in-depth analysis (problem solution)
2022-07-06 03:23:00 【Little snail rushes towards】
Preface
do person : Little snail rushes forward
quotes : I can accept failure , But I can't accept giving up
If you think the blogger's article is good , Please give the thumbs-up , Collection , Follow and support bloggers . If you find any problems, you are welcome * Please make corrections in the comments section .
Catalog
about C Language pointers are very important , In order to understand pointer more thoroughly , Now I'll share some pointers , Play with the pointer in the analysis of the topic .
Before doing the problem , We need to understand what array names are .
Array name
Usually refers to the address of the first element
In two special cases , Refers to the address of the entire array
1 & Address array name
2 sizeof( Array name )( alone )
One dimensional array
// One dimensional array
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a));//a Refers to the entire array name , So the byte size is 4*4=16
printf("%d\n", sizeof(a + 0));//a Refers to the address of the first element ,a+0 Indicates the number of bytes skipped 4*0=0, Point to 1, So the byte size is 4
printf("%d\n", sizeof(*a));//a Refers to the address of the first element , Yes *a find 1,1 The byte size of is 4
printf("%d\n", sizeof(a + 1));//a Refers to the address of the first element ,a+1 Indicates the number of bytes skipped 4*1=4, Point to 2, So the number of bytes is 4
printf("%d\n", sizeof(a[1]));//a[1]-->*(a+1), find 2, So the number of bytes is 4
printf("%d\n", sizeof(&a));//&a Represents the address of the whole array , The address in 32 The size of each platform is 4 Bytes
printf("%d\n", sizeof(*&a));//&a Represents the address of the whole array ,*&a Find each element of the array , Byte size 4*4=16
printf("%d\n", sizeof(&a + 1));//&a+1, It still indicates that the size of the address is 4 Bytes
printf("%d\n", sizeof(&a[0]));//&a[0]-->&*(a+0), Express &1, Take out 1 The address of , The size of the address is still 4 Bytes
printf("%d\n", sizeof(&a[0] + 1));//&a[0]+1 Express &2, The byte size is 4
For one-dimensional arrays , We must always remember , What is the meaning of array .
stay 32 Bit platform Next The size of the address is always 4 Bytes size
A character array
There are two initial situations for character arrays , Now we will experience the topic in detail .
situation 1
sizeof Question type
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));// The size of the entire array , Each element of the array is char type , The size is 1 Bytes , therefore 1*6=6
printf("%d\n", sizeof(arr + 0));//arr+0 Refers to the address of the first element , therefore 4 Bytes
printf("%d\n", sizeof(*arr));//*arr find 'a', therefore 1 Bytes
printf("%d\n", sizeof(arr[1]));//arr[1]-->*(arr+1), find 'b', therefore 1 Bytes
printf("%d\n", sizeof(&arr));//&arr Represents the address of the whole array , So the byte size is 4 Bytes
printf("%d\n", sizeof(&arr + 1));//&arr+1 It means to get the address after the array , therefore 4 Bytes
printf("%d\n", sizeof(&arr[0] + 1));// Empathy , therefore 4 Bytes
strlen Question type
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", strlen(arr));//'\0' The location is undetermined , Random value
printf("%d\n", strlen(arr + 0));// Random value
printf("%d\n", strlen(*arr));// There is a problem with the transmission , Unable to evaluate -->strlen(a)-->strlen(97)( Wild pointer )
printf("%d\n", strlen(arr[1]));//arr[1]-->*(arr+1), There is a problem with the transmission , Unable to evaluate -->strlen(b)-->strlen(98)
printf("%d\n", strlen(&arr));//&arr The address of the entire array , Random value
printf("%d\n", strlen(&arr + 1));// Random value -1*6
printf("%d\n", strlen(&arr[0] + 1));//&*arr[0]+1--&*(arr+0)+1, Random value -1
inductive
sizeof It's an operator, which asks for the size of bytes ,strlen Is the function of the size of the string .
char arr[] = { 'a','b','c','d','e','f' };// This initialization method does not add '\0'.
arr[0] Can be equivalent to *(arr+0)
situation 2
sizeof Question type
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));// Find the size of the entire array , So bytes 7
printf("%d\n", sizeof(arr + 0));// Find the size of the address of the first element , So bytes 4
printf("%d\n", sizeof(*arr));// Find the size of the first element , So bytes 1
printf("%d\n", sizeof(arr[1]));//arr[1]-->*(arr+1), seek b Size , So bytes 1
printf("%d\n", sizeof(&arr));// Find the size of the entire array address , So bytes 4
printf("%d\n", sizeof(&arr + 1));// Skip the size of the entire array address , So bytes 4
printf("%d\n", sizeof(&arr[0] + 1));//&arr[0]+1-->&*(arr+0)+1, So bytes 4
strlen Question type
char arr[] = "abcdef";
printf("%d\n", strlen(arr));// Pass the address of the first element of the array , So there is 6 Characters
printf("%d\n", strlen(arr + 0));// ditto ,6 Characters
printf("%d\n", strlen(*arr));// There is a problem with the transmission , No calculation
printf("%d\n", strlen(arr[1]));// There is a problem with the transmission , No calculation
printf("%d\n", strlen(&arr));// Pass the address of the whole array ( The same value as the address of the first element ), therefore 6 Characters
printf("%d\n", strlen(&arr + 1));// Skip array , After '\0' The location of is uncertain , So it's a random value
printf("%d\n", strlen(&arr[0] + 1));// Pass on b The address of , therefore 5 Characters
inductive
char arr[] = "abcdef";// There is... At the end of the character '\0'
Constant string
sizeof Question type
char* p = "abcdef";
//printf("%d\n", sizeof(p));//a The address of , So bytes 4
//printf("%d\n", sizeof(p + 1));//b The address of , So bytes 4
//printf("%d\n", sizeof(*p));// character a, So bytes 1
//printf("%d\n", sizeof(p[0]));//p[0]-->*(p+0), character a, So bytes 1
//printf("%d\n", sizeof(&p));// Get the address of the entire constant string , So bytes 4
//printf("%d\n", sizeof(&p + 1));// Point to the address after the constant character , So bytes 4
//printf("%d\n", sizeof(&p[0] + 1));//&*(p+0)+1-->&b, So bytes 4
strlen Question type
char* p = "abcdef";
printf("%d\n", strlen(p));//6 Give character
printf("%d\n", strlen(p + 1));// Pass on b Your address used to , character 5
printf("%d\n", strlen(*p));// There is a problem with the transmission , No calculation
printf("%d\n", strlen(p[0]));// There is a problem with the transmission , No calculation
printf("%d\n", strlen(&p));// hold a Send the address of the address , meet '\0' The location of is uncertain , Random value
printf("%d\n", strlen(&p + 1));// hold b Send the address of the address , meet '\0' The location of is uncertain , Random value
printf("%d\n", strlen(&p[0] + 1));// Pass on b The address of , therefore 5 Characters
inductive
Variable name of constant string Is the first address of the constant string .
&p about strlen In terms of functions , What passed is a secondary pointer , First of all, the address of the first address is stored in memory. I don't know if it is Big end storage still Small end storage , This makes you encounter '\0' The position of cannot be judged , So use strlen The result is a random value .
Two dimensional array
int a[3][4] = { 0 };
printf("%d\n", sizeof(a));//a Indicates that the size of the entire array is calculated ,48
printf("%d\n", sizeof(a[0][0]));// Calculate the size of the first element ,4
printf("%d\n", sizeof(a[0]));//a[0]-->*(a+0), Indicates the size of the first row of the count 16
printf("%d\n", sizeof(a[0] + 1));// Calculate the second line 1 Size of element addresses 4
printf("%d\n", sizeof(*(a[0] + 1)));// Count the second line 1 The size of an element 4
printf("%d\n", sizeof(a + 1));// Calculate the second line 1 Size of element addresses 4
printf("%d\n", sizeof(*(a + 1)));// Calculate the size of the second line 16
printf("%d\n", sizeof(&a[0] + 1));// Calculate the size of the address in the second line 4
printf("%d\n", sizeof(*(&a[0] + 1)));// Calculate the size of the second line 16
printf("%d\n", sizeof(*a));// Calculate the size of the first row 16
printf("%d\n", sizeof(a[3]));// Although the array is out of bounds , But what we ask is that the size of bytes is ok , It is equivalent to finding the number of arrays N The address of the line ,16
inductive
The array name of the two-dimensional array points to the... Of the array 1 That's ok
summary
The meaning of array names :
1. sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .
2. & Array name , The array name here represents the entire array , It takes out the address of the entire array .
3. In addition, all array names represent the address of the first element .
If you like , Let's support bloggers for three times !
边栏推荐
猜你喜欢
Python implementation of maddpg - (1) openai maddpg environment configuration
Buuctf question brushing notes - [geek challenge 2019] easysql 1
Performance analysis of user login TPS low and CPU full
[Li Kou] the second set of the 280 Li Kou weekly match
Modeling specifications: naming conventions
深入探究指针及指针类型
My C language learning record (blue bridge) -- under the pointer
Research on cooperative control of industrial robots
2.2 STM32 GPIO操作
ERA5再分析资料下载攻略
随机推荐
Linear regression and logistic regression
指针笔试题~走近大厂
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
Redis cache breakdown, cache penetration, cache avalanche
Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
Detailed use of dbutils # yyds dry goods inventory #
NR modulation 1
Restful style
Buuctf question brushing notes - [geek challenge 2019] easysql 1
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
电机控制反Park变换和反Clarke变换公式推导
Mysql database operation
Derivation of anti Park transform and anti Clarke transform formulas for motor control
jsscript
NR modulation 1
银行核心业务系统性能测试方法
Audio audiorecord binder communication mechanism
Deno介绍
Pytorch基础——(1)张量(tensor)的初始化
MPLS experiment