C language learning notes - first sight pointer

Posted by tmharrison on Tue, 02 Nov 2021 14:42:51 +0100

Function of pointer

1. Used as a parameter when large data needs to be passed in
2. Operate on the array after passing in the array
3. Function returns more than one result
4. You need a function to modify more than one variable
5. Dynamic request memory
...

Operator &*

Take address operator &:
scanf("%d",&i) ; Inside&
&The function of is to obtain the address of the variable, and its operand must be a variable

#include<stdio.h>
int main()
{
	int i=0;
	printf("%p",&i);//Output the address of variable i in hexadecimal form 
	return 0;
}

If the operand is not a variable, such as a+b, a + +, + + A, an error will be reported

Address of adjacent variable:
for example

#include<stdio.h>
int main()
{
	int i=0;
	int j;
	printf("%p\n",&i);//Output the address of variable i in hexadecimal form 
	printf("%p",&j);
	return 0;
}


We can see that one of the addresses is mantissa 1C and the other is 18. In hexadecimal, there is a difference of 4 between 8 and C. We know that int type accounts for 4 bytes, which means that the two variables are adjacent in memory. Looking further, we can see that i defined first is
1C, later defined J is 18, indicating that i is at a higher place and j is at a lower place in the memory. This is because i and j are local variables. They are allocated in the memory stack, and the memory allocated in the stack is allocated from top to bottom. (to be learned later)

Address of array:

#include<stdio.h>
int main()
{
	int a[10];//Define an array 
	printf("%p\n",&a);
	printf("%p\n",a);
	printf("%p\n",&a[0]);//Get the address of the 0th element of array a 
	printf("%p\n",&a[1]);//Get the address of the first element of array b 
	return 0;
}


It can be seen from the output result that the first three addresses are the same, which means that when directly writing the address of the array name, the address of the first element of the array is taken by default, and the & symbol can not be added (the association between the array and the pointer will be mentioned below). If you take the address of an element in the array, you only need to specify the element number in []. According to the observation, the addresses of adjacent elements in the array are also adjacent.

Indirect operator *:
A pointer is a variable that holds the address

#include<stdio.h>
int main()
{
	int i=1000;
	int*p = &i;//Variable p defining pointer type is used to store the address of variable i 
	printf("%d",*p);//*It can be understood as the value, * p is the value of the variable address pointed to by the pointer variable p 
	return 0;
}


Note: if int * P and q are defined in this way, it means that a pointer variable p and an integer variable q are defined

Pointer as parameter:

#include<stdio.h>
void f(int *p);//Define a function f that takes a pointer as a parameter 

int main()
{
	int i=1000;
	printf("i =%p\n",&i);//Print i's address 
	f(&i);//Call function f 
	return 0;
}

void f(int*p)
{
	printf("p =%p\n",p);//Print p's address 
}


Knock on the blackboard and draw the key points
We know that operating on a variable in a function will not change the value of the variable in the main function, but it can be done with a pointer.

#include<stdio.h>
void f(int *p);//Define a function f that takes a pointer as a parameter 

int main()
{
	int i=1000;
	printf("i =%p\n",&i);//Print i's address 
	printf("i=%d\n",i);
	f(&i);//Call function f
	printf("i=%d",i); 
	return 0;
}

void f(int*p)
{
	printf("p =%p\n",p);//Print p's address 
	*p=233;
}

It was the last code. This time, we assigned * p to 233 in the function, and then something magical happened:

The value of i changed from 1000 to 233. The reason is that using pointers, we fundamentally change the value of variables.
Practice: the problem of exchanging two variables when I first learned C language

#include<stdio.h>
void f(int *pa,int *pb);//Define a function f that takes a pointer as a parameter 

int main()
{
	int a=1000;
	int b=233;
	f(&a,&b);
	printf("a=%d  b=%d",a,b);
	return 0;
}

void f(int *pa,int *pb)
{
	int tmp=0;
	tmp=*pa;
	*pa=*pb;
	*pb=tmp;
}

Pointer application scenario: function returns multiple values

Because there is only one return value of a function, multiple values can be brought back with a pointer
For example: find the largest and smallest number in the array

#include<stdio.h>
void minmax(int a[],int len,int *min,int *max);//Define a function f that takes a pointer as a parameter 

int main()
{
	int min;
	int max;
	int a[10]={1,2,3,4,5,66,78,90,19,28};
	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)
{
	*min=*max=a[0];
	for(int i=1;i<len;i++)
	{
		if(*min>a[i])
		{
			*min=a[i];
		}
		if(a[i]>*max)
		{
			*max=a[i];
		}
	}
}

Common pointer errors

The pointer variable is defined, and the pointer is used before pointing to any variable.
The pointer must point to the address of a variable before * p can be used to operate on the value of the variable.

Array pointer Association

The array in the function parameter table is actually a pointer!!!
In the parameter table, * ar is equivalent to ar []

Array variables are special pointers
Array variables themselves express addresses, so
int a[10]; int *p=a; // No need to & take the address (although it is used, it will not report an error)
However, the cell expression of an array is a variable, so you need to use & to get the address
For example, to get the addresses of three elements in array a, you must write & A [3];

[] operators can be used for arrays or pointers
p[0]==a[0]
For example:

#include<stdio.h>
void minmax(int a[],int len,int *min,int *max);//Define a function f that takes a pointer as a parameter 

int main()
{
	int min;
	int max;
	int a[10]={1,2,3,4,5,66,78,90,19,28};
	minmax(a,sizeof(a)/sizeof(a[0]),&min,&max);
	printf("min=%d  max=%d\n",min,max);
	int *p=&min;//Define a pointer variable p to point to the address of the variable min
	printf("*p=%d\n",*p);//Print the value stored in the min address of the variable pointed to by the pointer p
	printf("p[0]=%d\n",p[0]);//Print the value of array p[0]
	return 0;
}

void minmax(int a[],int len,int *min,int *max)
{
	*min=*max=a[0];
	for(int i=1;i<len;i++)
	{
		if(*min>a[i])
		{
			*min=a[i];
		}
		if(a[i]>*max)
		{
			*max=a[i];
		}
	}
}


We didn't define an array. It was invented by us, but it can be compiled and run. This shows that we actually regard min as an array of int type, and the pointer p points to this array. (I don't quite understand this one...)

That's it for today. Continue next time.

Topics: C