当前位置:网站首页>Analysis of pointer
Analysis of pointer
2022-06-12 05:28:00 【Gradually every day, I miss my hometown a little more】
c The point of is in the pointer , It gives user You have too much power .
Start with an example :
strcut test_t
{
int a;
char b;
int c;
};
char *p; int *p1; struct test_t *p2;
printf("%d",sizeof(p))
==> Output is 4
printf("%d",sizeof(*p)
==> Output is 1
printf("%d",sizeof(p1))
==> Output is 4
printf("%d",sizeof(*p1)
==> Output is 4
printf("%d",sizeof(p2))
==> Output is 4
printf("%d",sizeof(*p2)
==> Output is 12
From the above examples, we can see that the gods and Demons : 1. The pointer itself accounts for 4 Bytes . *p Refers to the address that points to The length of the variable , What you pay attention to 1 To 3 Different
Advanced : char **pp
printf("%d %d %d %d\n",sizeof(char *),sizeof(**pp),sizeof(*pp),sizeof(pp))
What is the output ?
4,1,4,4
This is supposed to be char * Hour indicates pointer 4 Bytes ,**pp Indicates the value pointed to by the pointer char by 1,*pp Indicates the address value 4,pp It is also the address value 4
char *p = (char *)malloc(4 *4);
char **p2=(char **)malloc(4*4)
What's the difference between the two ?
The answer is different , Because the double pointer is a Address This address stores the address value of a pointer . So its initialization must malloc two .
*p2=malloc(n); *(p2+1)=malloc(n); .... *(p2+3)=malloc(n);
In this way , Try to understand the pointer . The pointer has two meanings , One itself is a four byte variable . Second, he needs to point to an address . This is not easy to understand , The double pointer is The pointer to the pointer , that He doesn't have an address himself , Then he points to an address , A double pointer refers to a pointer address , If you want to use this pointer, you have to point to an address .
Because the pointer only points to one address , The address he pointed to We need to analyze it by ourselves . Therefore, we can better use pointers by understanding the meaning of variable names of various types ;
Array : char arr[12] --> arr Represents an address value and &arr &arr[0] It's an address , Find out the ammunition arr[0..n] It's not the address value , That's it char Variable value Once declared Once the address is determined, it cannot be changed So we can think of it as a pointer constant . So it is wrong to copy arrays directly Equivalent to changing pointer constant .
int arr[12] --> arr It also means an address and &arr &arr[0] It's an address
Variable :char a --> a It means One char value ,&a It's the address
int a -->a It means One char value ,&a It's the address
char *p --> p Indicates the address value ,*p Express char value
Structure : struct test a --> a Represents a variable name It's not the address ,&a It is the structure The value of the first variable of , because The structure has problems with it , Although his overall address is continuous, but due to its problems The internal variable addresses are not continuous . So directly %d %d a a+1 To print test a The first variable of Is the second variable OK ? Can not be because a+1 Express The step size of is not in It is sizeof(struct test), So I drink too much +1 Step size of Make it clear
The pointer : char *p --> p It means the address .
notes: Can two arrays be copied directly ? for example char a[12]="test", c+har b[12] char c[8]; b=a, c=a?
Is there a problem in this way ? No, because c Once declared, the array in is a definite and unchangeable address constant pointer , therefore c It is stipulated that Arrays are assigned directly ?
Well, the two one. struct Between variables , Because his name is not an address And his address is continuous . In a structure without pointer member variables It can be assigned directly , If there is a pointer member variable, it is not allowed , One if a.c malloc Let the back handle a Assign to another struct variable . that a.c free After another variable c The pointer address is also free 了 This leads to a series of troubles , therefore c It also stipulates that the structure cannot be directly assigned .
As long as it's the address use memcpy No matter what type it is Copy .
边栏推荐
- org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
- 论文阅读_图神经网络GIN
- CODIS long link test
- Thingsboard view telemetry data through database
- Detailed usage of vim editor
- Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
- Sv806 QT UI development
- 深入理解异步编程
- How to deploy dolphin scheduler Apache dolphin scheduler 1.2.0 in cdh5.16.2
- JS to determine whether the tags of multiple classes are empty
猜你喜欢

Performance test - Analysis of performance test results

Classes and objects, methods and encapsulation

SQL transaction

Codis 3. X expansion and contraction

16. 最接近的三數之和

Pupanvr- an open source embedded NVR system (1)

Thingsboard create RCP widget

CODIS long link test

Selenium crawler automatically captures TOEFL test position of NEEA website

公司注册认缴资金多久
随机推荐
Soil type, soil texture, soil nutrient and change data, soil organic matter, soil pH, soil nitrogen, phosphorus and potassium
Pupanvr- an open source embedded NVR system (1)
Performance & interface test tool - JMeter
Servlet core
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
Servlet core technology
Necessary for Test Engineer -- package capturing tool fiddler
Vivado HLS introductory notes
Set common methods
Accumulated temperature spatial distribution data, temperature distribution data, sunshine data, rainfall distribution, solar radiation data, surface runoff data, land use data, NPP data, NDVI data
It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
Lvgl8.1 hi3536c platform use
Font conversion optimization
Performance test - GTI application service performance monitoring platform
16. Somme des trois plus proches
49. ugly number
Selenium crawler automatically captures TOEFL test position of NEEA website
MySQL5.7.21 Build For ARM
Codec of ASoC framework driven by alsa
Computer network connected but unable to access the Internet