当前位置:网站首页>The basis of C language grammar -- pointer (multidimensional array, function, summary) learning

The basis of C language grammar -- pointer (multidimensional array, function, summary) learning

2022-06-26 09:50:00 Tanzanian auduvi Canyon expert

Normal pointer :

 Two dimensional array , Generally, there are several rows and columns ;
 If more than one ordinary pointer is defined , This pointer only represents the address of the row , Instead of an element address , such as :
	int a[3][3]={
    1,2,3,4,5,6,7,8,9};
	int *p;
	p=a; // 
	printf("%d\n",*p); // 1  The element values of the first row and the first column  
	p=a+1;
	printf("%d\n",*p); // 4  The element values in the first column of the second row  

 You can define a pointer to a specified address :
	int a[3][3]={
    1,2,3,4,5,6,7,8,9};
	int *p;
	p=&a[1][1]; //  The address of the element in the second row and the second column  
	printf("%d\n",*p); // 5  The element values of the first row and the first column  
	p++;
	printf("%d\n",*p); // 6  The element values in the second row and the third column  
	p=a+1; //  The second line  
	printf("%d\n",*p); // 4  The element values in the first column of the second row  


 Insert picture description here

 Insert picture description here

int main(int argc, char *argv[]) {
    
	/* 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 */
	
	int a[5][5];
	int i,j;
	for(i=0;i<5;i++){
    
		for(j=0;j<5;j++){
    
// a[i][j]=i-j+1; //  Non pointer version  
			*(a[i]+j)=i-j+1; //  Pointer version  
			*(*(a+i)+j)=i-j+1; //  Pointer version  

			if(i>=j){
    
// printf(" %d", a[i][j]); //  Non pointer version  
// printf(" %d", *(a[i]+j)); //  Pointer version  
				printf(" %d", *(*(a+i)+j)); //  Pointer version  

			}
		}
		printf("\n");
	}
	
	
	return 0;
}

Array pointer :

 Define line pointer ( Array pointer ): You can use *(*(p+i)+j, Otherwise, the report will be wrong , Only use *(*(a+i)+j
int a[3][3]={
    1,2,3,4,5,6,7,8,9};
int (*p)[3]; //  Array row pointer 

 Insert picture description here
 Insert picture description here

Several pointer operations on arrays :

 Insert picture description here
Notice the commas in the figure , The execution order is from right to left , If it is a comma, change it to a multiplication sign , It's from left to right

int a[]={
    1,2,3},y,*p=&a[2];
y=*--p;
y Value : 2  
 Logic :p The initial pointing subscript is 2 The address of , First --, Move to subscript 1, then * Value , The table below for 1 The address of , The value is 2


int a[]={
    1,2,3,4,5,6},*p=a;
*p++ * *++p  The value of is :3
 Logic : Multiplication sign , From left to right , First *p=1, Then add the pointer 1, In execution ++p, The pointer is adding 1, And then take *++p=3 ,  Last  1*3=3


A function pointer :

int max(int a, int b){
    }
int (*p)()
max(a,b) --> (*p)(a,b)

Pointer array :

Definition :
Each subscript variable is a pointer variable , It's an address , It is mainly used to handle variable length multiple strings .

understand : Pointer array is not the same concept as the previous array pointer , Pointer arrays are special in that all elements are an address

style :
int *b[3]={ a,&a[1] }
 Insert picture description here

Pointer summary :

 Insert picture description here

//  Area of circle  

double area(int r){
    
	return 3.14*r*r;
}


int main(int argc, char *argv[]) {
    
	double (*p)(); //  Note that the type must be consistent with the function type , If the function is double, Here is int, The result will be  4199728.00000 
	p=area;
	int i=2;
	double result;
	result=(*p)(i);
	printf("%f",result); // 12.560000 

	
	return 0;
}
原网站

版权声明
本文为[Tanzanian auduvi Canyon expert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260911168420.html