当前位置:网站首页>[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
边栏推荐
- Analyze comp-206 advanced UNIX utils
- [shutter] shutter layout component (opacity component | clipprect component | padding component)
- Accounting regulations and professional ethics [17]
- Number of DP schemes
- MySQL learning notes (Advanced)
- Go web programming practice (2) -- process control statement
- Cloud computing technology [2]
- Common authority query instructions in Oracle
- Construction and maintenance of business website [2]
- kernel_ uaf
猜你喜欢
kernel tty_ struct
treevalue——Master Nested Data Like Tensor
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
qwb2018_ core kernel_ rop
[question brushing diary] classic questions of dynamic planning
Check the confession items of 6 yyds
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
26 FPS video super-resolution model DAP! Output 720p Video Online
Cardinality sorting (detailed illustration)
rwctf2022_ QLaaS
随机推荐
A river of spring water flows eastward
MySQL learning record (6)
China plastic bottle market trend report, technological innovation and market forecast
Who do you want to open a stock account? Is it safe to open a mobile account?
[shutter] statefulwidget component (image component | textfield component)
Baidu sued a company called "Ciba screen"
7. Build native development environment
Accounting regulations and professional ethics [16]
When Valentine's Day falls on Monday
Welfare, let me introduce you to someone
MySQL learning record (3)
China's Micro SD market trend report, technology dynamic innovation and market forecast
Structured text language XML
Internet Explorer ignores cookies on some domains (cannot read or set cookies)
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
[12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
[C language] [sword finger offer article] - replace spaces
BitSet complement
Free open source web version of xshell [congratulations on a happy new year]