Array types and the nature of multidimensional arrays

1 Array Concept

 1) Element type angle: the array is an ordered collection of variables of the same type, and the memory space occupied by the pointer variable is tested.
 2) Memory angle: a large piece of memory space associated with
the
array. The number of array elements can be specified explicitly or implicitly. Analysis array initialization {0} compared with memset.
{0} takes about the same time as memset, but {0} may have portability problems. Although most compilers see that {0} initializes all arrays to 0, it is not guaranteed that all compilers implement this way. ; The memset method is recommended.

Technical blind spots of array names
 1) The address of the first element of the array and the address of the array are two different concepts
 2) The array name represents the address of the first element of the array, which is a constant.
 The explanation is as follows: The variable is essentially an alias of the memory space. Once the array is defined, the memory is allocated and the memory is fixed. Therefore, the array name cannot be modified after it is named.
 3) The address of the first element of the array is equal to the address value of the array
 4. How to get the address of the entire one-dimensional array?
C language specification:
Int a[10];
printf("Get the address of the entire array a: %d \n", &a );
printf("The address of the first element of the array a: %d \n", a );

2Array type, array pointer type, array pointer type variable

Array type
 1 The data type is divided into basic and non-basic, and the thinking angle should change
 2 The array in C language has its own specific type
 The type of the array is determined by the element type and the size of the array
 Example: int array[5] is of type int[5]

    typedef int(MYINT5)[5];   //int
    typedef float(MYFLOAT10)[10];
    数组定义:
    MYINT5i Array;   int array[5];
    MYFLOAT10fArray

Array pointer type
 Array pointer is used to point to an array.

There are three ways
to define an array pointer: 1) Define an array pointer by an array type:
typedef int(ArrayType)[5]; int *a
ArrayType* pointer;

{
    int a[5];
    //声明一个数组类型
    typedef int(MYINT5)[5];
    //用数组类型 加*,定义一个数组指针变量
    MYINT5 *array;
    array = &a;
    for (i=0; i<5; i++)
    {
        (*array)[i] = i;
    }
    //
    for (i=0; i<5; i++)
    {
        printf("\n%d %d", a[i], (*array)[i]);
    }
}

2) Declare an array pointer type
typedef int (*MyPointer)[5];
MyPointer myPoint;

{
    int b[5];
    //声明一个数组指针类型
    typedef int (*MyPointer)[5];
    //用数组指针类型,去定义一个变量
    MyPointer mypoint ;
    mypoint= &b;    
    for (i=0; i<5; i++)
    {
        (*mypoint)[i] = i;
    }
    //
    for (i=0; i<5; i++)
    {
        printf("\n%d %d", b[i], (*mypoint)[i]);
    }
}

3) Direct definition: int (*pointer)[n];
pointer is the array pointer variable name
type is the type of the pointed array
n is the size of the pointed array

{
    int c[5];
    //直接声明一个数组指针变量
    int (*pointer)[5] = &c;
    for (i=0; i<5; i++)
    {
        (*pointer)[i] = i;
    }
    for (i=0; i<5; i++)
    {
        printf("\n%d %d", c[i], (*pointer)[i]);
    }
}

3 The essence of multi-dimensional array technical deduction

void main222()
{
int a[3][5];

int c[5]; //&c + 1;  表示指针向后移4*5个单位
int b[10]; //b代表数组首元素的地址 &b代表这个数组的地址 &b+1相当于 指针后移4*10个单位

//a的本质是一个数组指针,每次往后跳一维的维数    
printf("a:%d, a+1:%d \n", a, a+1); //4*5
}

char cbuf[30]; // cbuf (level 1 pointer) represents the address of the first element of the array. &cbuf (secondary pointer) represents the address of the entire array.

char array[10][30]; //array is
the first address of the secondary pointer array two-dimensional array
(array+i) //equivalent to the array address of the entire i-th row//secondary pointer &cbuf
(*(array+ i))//The first address of the one-dimensional array cbuf
(*(array+i))+j //equivalent to the address of the i-th row and the j-th column "=="&array[i][j]
*(((*( array+i))+j) //equivalent to the value of the i-th row and the j-th column <====>array[i][j]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644585&siteId=291194637