当前位置:网站首页>[use of pointer and pointer and array]
[use of pointer and pointer and array]
2022-07-02 21:34:00 【I want to watch the snow with you】
One 、 Pointer to the variable
Pointer variable is the variable that holds the address
int i;
int *p=&i;
//* Is a unary operator used to access variables on the address represented by the value of the pointer
// It can be left-hand value or right-hand value , That is to say, it can be placed in = On the left of, let's assign values It can also be placed in = On the right, let's read its value So let's take this i The address of is given to the pointer p,p What is kept is i The address of , At this time, we will say p Yes i.
The value of a normal variable is the actual value , The value of the pointer variable is the address of the variable with the actual value .

Through the pointer, we access i
As a pointer to a parameter
void f(int *p)
// Get the address of a variable when called
int i;
int *p=&i;
f(p);
// You can access the outside through the pointer in the function i
// Access means reading or writing
Let's try this thing

We still think that what happens is the transmission of values ,p(&i) This address value is passed into the function , So this is still the transmission of values , Because the address came in , Through this address, we can access the outside of the function in this way i Variable ,p yes i The address of , therefore *p It stands for i, In this way, we can modify this i, Do the above *p=123 This operation is actually right i do .
Pointer operator & *
The two interact
*&point->*(&point)->*(point The address of )-> Get the variable at that address ->point
&*point->&(*point)->&(point)-> obtain point The address of , That is to say point->point
Two 、 Pointer use
Pointer application scenario 1
Function returns multiple values , Some of them can only be returned by pointer
The parameter passed in is actually the variable that needs to save the returned result
Take a chestnut
#include<stdio.h>
void minmax(int a[],int len,int *min,int *max);//len Is the number of array elements
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,12,18,55,88};
int min,max;
minmax(a,sizeof(a)/sizeof(a[0]),&min,&max);
printf("min=%d,max=%d",min,max);
return 0;
}
void minmax(int a[],int len,int *min,int *max)
{
int i;
*min=*max=a[0];
for(i=1;i<len;i++)
{
if(*min>a[i])
*min=a[i];
if(*max<a[i])
*max=a[i];
}
}

This is a very common scenario of pointer Application : The result of my function is more than one , Then I pass in the address of the variable of the result I want to receive through the pointer , Let the function handle these variables for me , Pass the value back , Although these two parameters are from the main function main Passed in , But their function is to bring out the results
Pointer application scenario 2
Function returns the state of the operation , The result returns... Through the pointer
A common routine is to let the function return a special value that does not belong to the valid range to indicate an error : for instance -1 or 0
But when any number is a valid possible result , We have to go back separately
Take a chestnut
#include<stdio.h>
int divide(int a,int b,int *result);// Define a division function
int main()
{
int a,b;
scanf("%d%d",&a,&b);
int c;
if(divide(a,b,&c))
{
printf("%d/%d=%d\n",a,b,c);
}
return 0;
}
int divide(int a,int b,int *result)
{
int flag=1;
if(b==0)// Divisor is 0 Then division is meaningless
flag=0;
else
*result=a/b;
return flag;
}The result of division is transmitted through the pointer , Division succeeded flag return 1,

If the divisor is 0 Then division is meaningless The function returns 0 if(divide()) No operation

The most common error with pointers
That is, a pointer is defined and uninitialized I started using
int *p;
*p=12;// Dabao !This is the legendary wild pointer , There is no object ;
3、 ... and 、 Pointers and arrays
If we pass an array to the function through the parameters of the function , So what does the number of incoming functions consist of ?
If we pass a common variable Then the function receives a value , If you pass a pointer Then the parameter receives a value , Only this value is the address .
How about passing an array ?
We continue to use the just minmax function
#include<stdio.h>
void minmax(int a[],int len,int *min,int *max);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,12,18,55,88};
int min,max;
printf("in main sizeof(a)=%d\n",sizeof(a));// stay main Function sizeof(a) Value
minmax(a,sizeof(a)/sizeof(a[0]),&min,&max);
printf("min=%d,max=%d",min,max);
return 0;
}
void minmax(int a[],int len,int *min,int *max)
{
int i;
printf("in minmax sizeof(a)=%d\n",sizeof(a));// After passing in the function sizeof(a) Value
*min=*max=a[0];
for(i=1;i<len;i++)
{
if(*min>a[i])
*min=a[i];
if(*max<a[i])
*max=a[i];
}
}However, a subtle change has taken place

As can be seen in the main Function and in minmax Function sizeof(a) There is a change
stay minmax Function sizeof(a) The value of is 4, Just the same size as a pointer
Let's try again
#include<stdio.h>
void minmax(int a[],int len,int *min,int *max);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,12,18,55,88};
int min,max;
printf("in main sizeof(a)=%d\n",sizeof(a));
printf("in main a=%p\n",a);// look here****
minmax(a,sizeof(a)/sizeof(a[0]),&min,&max);
printf("min=%d,max=%d",min,max);
return 0;
}
void minmax(int a[],int len,int *min,int *max)
{
int i;
printf("in minmax sizeof(a)=%d\n",sizeof(a));
printf("in minmax a=%p\n",a);//look here****
*min=*max=a[0];
for(i=1;i<len;i++)
{
if(*min>a[i])
*min=a[i];
if(*max<a[i])
*max=a[i];
}
}
This time we are looking at main Li He minmax in a The address of

Everyone is exactly the same , The explanation is in minmax This one inside a Arrays are actually main Inside a Array It's not exactly the same . They are the same
Let's continue the experiment
void minmax(int a[],int len,int *min,int *max)
{
a[0]=10000;// stay minmax Function lining a[0]=10000;
So in fact, in the function parameters a[] It's just a pointer , It looks like an array
So even in void minmax(int a[],int len,int *min,int *max) The parameter int a[] Change it to int *a The compilation can still be run correctly
So the array in the function parameter table , It's actually a pointer
// So the following two forms are equivalent ;
int minmax(int *a)
int minmax(int a[])Array variables are special pointers
Array variables themselves express addresses
// therefore a The address of ==&a[0]
int a[10];
int *p=a;// Don't need to use & Address fetch
// But the elements in the array represent a single variable Need to use & Address fetch
//[]y Operators can do... With arrays You can also do
int *p=a;
//p[0]<==>a[0]
//* Operator can also do... On an array
*a=25;//a[0]==25边栏推荐
- China's log saw blade market trend report, technological innovation and market forecast
- 2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
- Construction and maintenance of business websites [9]
- China Indonesia advanced wound care market trend report, technological innovation and market forecast
- Research and Analysis on the current situation of China's clamping device market and forecast report on its development prospect
- Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
- Research Report on right-hand front door industry - market status analysis and development prospect forecast
- 5 environment construction spark on yarn
- China's noise meter market trend report, technical dynamic innovation and market forecast
- Construction and maintenance of business website [3]
猜你喜欢

How is LinkedList added?

kernel tty_ struct
![[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)](/img/04/4070d51ce8b7718db609ef2fc8bcd7.jpg)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)

treevalue——Master Nested Data Like Tensor
![[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)](/img/a7/0b87fa45ef2edd6fac519b40adbeae.gif)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)

kernel_ uaf
![[shutter] shutter layout component (opacity component | clipprect component | padding component)](/img/6b/4304be6a4c5427dcfc927babacb4d7.jpg)
[shutter] shutter layout component (opacity component | clipprect component | padding component)

如何防止你的 jar 被反编译?

Redis分布式锁故障,我忍不住想爆粗...
![[CV] Wu Enda machine learning course notes | Chapter 12](/img/c8/9127683b6c101db963edf752ffda86.jpg)
[CV] Wu Enda machine learning course notes | Chapter 12
随机推荐
Accounting regulations and professional ethics [19]
China Indonesia advanced wound care market trend report, technological innovation and market forecast
Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
MySQL learning record (8)
Internal/validators js:124 throw new ERR_ INVALID_ ARG_ Type (name, 'string', value) -- solution
Go web programming practice (1) -- basic syntax of go language
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of multi-channel signal conditioners in the global market in 2022
Micro SD Card Industry Research Report - market status analysis and development prospect forecast
Send blessings on Lantern Festival | limited edition red envelope cover of audio and video is released!
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
Capacity expansion mechanism of ArrayList
MySQL learning notes (Advanced)
China plastic bottle market trend report, technological innovation and market forecast
Research Report on the overall scale, major manufacturers, major regions, products and applications of sliding door dampers in the global market in 2022
Construction and maintenance of business website [1]
Accounting regulations and professional ethics [17]
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
Go cache of go cache series