basic understanding
#include <stdio.h>
#include <windows.h>
int main(){
SetConsoleOutputCP(65001);
int var=20;
int p=var;
printf("%d\n",var);
printf("%d\n",p);
printf("%p\n",&var);
printf("%p\n",&p);
return 0;
}
%p means the value of the pointer
As a result, although the values of var and p are the same, the two variables point to two memory addresses.
Then if you change the value of the var variable
#include <stdio.h>
#include <windows.h>
int main(){
SetConsoleOutputCP(65001);
int var=20;
int p=var;
printf("%d\n",var);
printf("%d\n",p);
printf("%p\n",&var);
printf("%p\n",&p);
printf("-------\n");
var=11;
printf("%d\n",var);
printf("%d\n",p);
printf("%p\n",&var);
printf("%p\n",&p);
return 0;
}
It can be seen that the value of var has changed, the value of p has not changed and the memory addresses of the two variables have not changed, which means that this is just a simple process of changing the value. If you want the variable p to change according to the value of var, Then p will point to the memory address of var
As follows, look at the code below to understand
#include <stdio.h>
#include <windows.h>
int main(){
SetConsoleOutputCP(65001);
int var=20;
int *p;
p=&var;
printf("%d\n",var);
printf("%d\n",*p);
printf("%p\n",&var);
printf("%p\n",p);
printf("-------\n");
var=11;
printf("%d\n",var);
printf("%d\n",*p);
printf("%p\n",&var);
printf("%p\n",p);
return 0;
}
var is (value | address) is (20 | &var)
*p is a pointer variable, pointing to the address of var, relative to var expressed by p is (*p | p) is (value | address)
pointer arithmetic
#include <stdio.h>
#include <windows.h>
int main(){
SetConsoleOutputCP(65001);
int var[]={10,100,200};
int *ptr;
ptr=&var[0];// 也可以是ptr=var
for(int i=0;i<3;i++){
printf("var[%d]值:%d\n",i,*ptr);
printf("var[%d]地址:%p\n",i,ptr);
ptr++;
}
return 0;
}
array of pointers
#include <stdio.h>
#include <windows.h>
#define MAX 3
int main(){
SetConsoleOutputCP(65001);
int var[MAX]={10,100,200};
int *ptr[MAX];
for(int i=MAX-1;i>=0;i--){
ptr[i]=&var[i];
printf("%d\n",*ptr[i]);
}
return 0;
}
string array pointer
#include <stdio.h>
#include <windows.h>
#define MAX 3
int main(){
SetConsoleOutputCP(65001);
char *name[]={
"小白",
"小红",
"小明"
};
for(int i=MAX-1;i>=0;i--){
// printf("%s\n",name[i]);
puts(name[i]);
}
}
pointer to pointer
#include <stdio.h>
#include <windows.h>
#define MAX 3
int main(){
SetConsoleOutputCP(65001);
int a=1;
int *b,**c;
b=&a;
c=&b;
printf("%d\n",a);
printf("%d\n",*b);
printf("%d\n",**c);
printf("%p\n",&a);
printf("%p\n",b);
printf("%p\n",c);
}
pass pointer to function
functions with pointers as arguments
#include <stdio.h>
#include <windows.h>
void countFun(int *s){
*s=1;
}
int main(){
SetConsoleOutputCP(65001);
int size=0;
countFun(&size);
printf("%d",size);
}
Functions that take pointers as parameters can also accept arrays as parameters
#include <stdio.h>
#include <windows.h>
double avg(int *arr,int size){
int count=0;
for(int i=0;i<size;i++){
count+=arr[i];
};
double avgVal=(double)count/size;
return avgVal;
}
int main(){
SetConsoleOutputCP(65001);
int arr[]={1,2,3,4,5,6};
int size=sizeof(arr)/sizeof(arr[0]);
double avgVal=avg(arr,size);
printf("%d",avgVal);
}
function return pointer
If you want to return in the function, you must use statci to define static variables. The usage of static in C/C++ is global variables and local variables | rookie tutorial
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* 要生成和返回随机数的函数 */
int * getRandom( )
{
static int r[10];
srand((unsigned) time(NULL));
for(int i=0;i<10;i++){
r[i]=rand()%10;
printf("%d\n",r[i]);
}
return r;
}
/* 要调用上面定义函数的主函数 */
int main ()
{
int *p;
p=getRandom();
printf("\n-------\n");
for(int i=0;i<10;i++){
printf("%d\n",*(p+i));
}
return 0;
}
function pointer
For example, the following is a normal function
#include <windows.h>
#include <stdio.h>
int cyc1(int x,int y){
return x>y?x:y;
}
int main(void){
SetConsoleOutputCP(65001);
int a,b,c;
printf("请输入3个整数:");
scanf("%d %d %d",&a,&b,&c);
int val=cyc1(cyc1(a,b),c);
printf("%d",val);
return 0;
}
then use the function pointer
#include <windows.h>
#include <stdio.h>
int cyc1(int x,int y){
return x>y?x:y;
}
int main(void){
SetConsoleOutputCP(65001);
int a,b,c;
int (*p)(int,int)=&cyc1;
printf("请输入3个整数:");
scanf("%d %d %d",&a,&b,&c);
int val=p(p(a,b),c);
printf("%d",val);
return 0;
}