Pointer manipulation of two-dimensional and multi-dimensional arrays

The key point is to understand the difference between pointers and arrays:
1. Pointers can be dereferenced, but arrays cannot.
2. Arrays are essentially addresses, pointers are also addresses, but arrays are constants that cannot be assigned, while pointers are variables that can be assigned.

Note that regardless of whether it is a two-dimensional array or a multi-dimensional array, the first dereference is dereferenced from the outermost layer, that is, the first-dimensional array.

*p = p[0]
*(p+1) = p[1]
*p+1 = p[1] + 1 //等价于p[1]元素的值加上整数1

Pointer access to two-dimensional array

#include <stdio.h>
void method(int (*p)[3],int n);
int main(){
    
    
    int a[2][3] =
    {
    
    {
    
    789,654,123},
    {
    
    357,753,951}};
    method(a,2);



}

void method(int (*array)[3],int n)
{
    
    
    int i,j;
    int (*p)[3];
    p = array;
    for(i=0;i<n;i++){
    
    
        for(j=0;j<3;j++)
        {
    
    

            printf("%d\n",*(*(p+i)+j));
        }
    }
    
}	

Pointer access to three-dimensional arrays

#include <stdio.h>
void method(int (*array)[2][3],int n);
int main(){
    
    
	int i,j,k;
	int data[2][2][3] = {
    
     {
    
     {
    
     999, 888, 777 }, {
    
     666, 555, 444} },
                      {
    
     {
    
     333, 222, 111 }, {
    
     100, 520,250 } } };
	int (*p)[2][3];
	p = data;   //data 实际上指向 data[0][0][0] ;
	
	for(i=0;i<2;i++)
	for(j=0;j<2;j++)
	for(k=0;k<3;k++){
    
    
		printf("%d\n",*(*(*(p+i)+j)+k));
	}


                          
}

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/112855173