Multidimensional arrays and multidimensional pointers

1. Pointer variables occupy a certain amount of space in memory

2. You can define a pointer to save the address value of the pointer variable

3. Pointers are also variables in essence, and there are also call-by-value and call-by-reference for pointers

#include <stdio.h>#include <malloc.h>int reset(char**p, int size, int new_size){    int ret = 1;    int i = 0;    int len = 0;    char* pt = NULL;    char* tmp = NULL;    char* pp = *p;        if( (p != NULL) && (new_size > 0) )    {        pt = (char*)malloc(new_size);                tmp = pt;                len = (size < new_size) ? size : new_size;                for(i=0; i<len; i++)        {            *tmp++ = *pp++;              }                free(*p);        *p = pt;    }    else    {        ret = 0;    }        return ret;}int main(){    char* p = (char*)malloc(5);        printf("%0X\n", p);        if( reset(&p, 5, 3) )    {        printf("%0X\n", p);    }        return 0;}

Two-dimensional array and two-level pointer

1. Two-dimensional arrays are arranged in a one-dimensional manner in memory

2. The first dimension in a two-dimensional array is a one-dimensional array

3. The second dimension in the two-dimensional array is the specific value

4. The array name of a two-dimensional array can be regarded as a constant pointer

5. The name of the two-dimensional array represents an array pointer. The two-dimensional array can be regarded as a one-dimensional array. Each element in the two-dimensional array is a one-dimensional array of the same type.

#include <stdio.h>int main(){ int a[5][5]; int(*p)[4];//Array pointer p = a; printf("%d\n", &p[4 ][2] - &a[4][2]);//Offset 18 minus offset 22}

Summarize:

1. There is only one-dimensional array in C language, and the size of the array must be determined as a constant at compile time

2. Array elements in C language can be any type of data, that is, the elements of an array can be another array

3. In C language, only the size of the array and the address of the first element of the array are directly determined by the compiler

Guess you like

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