The nature of multidimensional arrays

 

#include<stdio.h>
#include<string.h>

int main()
{
    int a[3][5];
    int i,j;
    int temp = 0;
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            a[i][j] = temp++;
        }
    }
    
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("a[i][j] = %d\n",a[i][j]);
        }
    }
    
    printf("a : %d, a + 1 : %d\n",a,a+1);
    printf("&a : %d, &(a + 1) : %d\n",a,a+1);
    return 0;
}

operation result:

 

 A two-dimensional array of pointers step is 20 (* 5 4: 5 * int type 4bit)

 

#include<stdio.h>
#include<string.h>

int main()
{
    int a[3][5];
    int i,j;
    int temp = 0;
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            a[i][j] = temp++;
        }
    }
    
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 5; j++)
        {
            printf("a[i][j] = %d\n",a[i][j]);
        }
    }
    
    {
        int (*pArray)[5];
        pArray = a;
        for(i = 0; i < 3; i++)
        {
            for(j = 0; j < 5; j++)
            {
                printf("pArray[i][j] = %d\n",pArray[i][j]);
            }
        }        
    }
    
    printf("a : %d, a + 1 : %d\n",a,a+1);
    printf("&a : %d, &(a + 1) : %d\n",a,a+1);
    return 0;
}

Compile, run successfully.

 

The nature of multidimensional array is an array of pointers name == "step, one-dimensional length.

 

First address (a + i) represents the i-th row

* (A + i) representative of a pointer to the first element of the i-th row address

* (A + i) + j representative a [i] [j] element

 

The first element of the first row address and the address of the first line are overlapping

 

*(a+i)+j == a[i][j]

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11688948.html