Multidimensional arrays and multidimensional pointers in C (31)

        We mentioned before that the essence of pointers is variables, and of course pointers will occupy a certain amount of memory space. We can define the pointer of the pointer to save the address value of the pointer variable, which is what we call a two-dimensional pointer.

        So why do you need pointers to pointers? The essence of a pointer is a variable, and there are also call-by-value and call-by-reference for pointers . Let's take a look at a sample code, the code is as follows

#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 = malloc(sizeof(int) * 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;
}

intmain()
{
    char* p = (char*)malloc(5);
    
    printf("%p\n", p);
    
    if( reset(&p, 5, 3) )
    {
        printf("%p\n", p);
    }
    
    free(p);
    
    return 0;
}

        We see that the function of this function is to reset the size of the requested memory space, and at the end print the address of the pointer before and after the reset. If the reset is successful, the address will be changed and the result will be as follows

picture.png

        So what exactly is a two-dimensional array? A two-dimensional array is arranged in the memory as a one-dimensional array, and its first dimension is a one-dimensional array. The second dimension is the specific value, and the array name of the two-dimensional array can also be regarded as a constant pointer . The figure below illustrates more vividly

picture.png

        So let's take the code as an example to analyze

#include <stdio.h>

void PrintArray(int a[], int size)
{
    int i = 0;
    
    printf("PrintArray : %d\n", sizeof(a));
    
    for(i=0; i<size; i++)
    {
        printf("%d\n", a[i]);
    }
}

intmain()
{
    int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    int* p = &a[0][0];
    
    int i = 0;
    int j = 0;
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d ", *(*(a + i) + j)); // *(a + i) ==> a[i]; *(a[i] + j) ==> a[i][j]
        }
        
        printf("\n");
    }
    
    printf("\n");
    
    PrintArray(p, 9);
    
    return 0;
}

        We see that the two-dimensional array a is defined on line 17 of the program, and the pointer p is defined on line 18, pointing to the address of the first element of the two-dimensional array. Next we print the array, on line 35 we call the PrintArray function to print the array. The result is as follows

picture.png

        We see that two-dimensional arrays are also distributed one-dimensionally in memory . Then the one-dimensional array name represents the address of the first element of the array, and the two-dimensional array name also represents the address of the first element of the array . For example: the type of int a[5]  a is int* ; the type of int m[2][5] m is int(*)[5] ;   

        A two-dimensional array name can be regarded as a constant pointer to the array, and it can also be regarded as a one-dimensional array, except that each element in the array is also an array of the same type . Let's take a look at how to apply for a two-dimensional array, the code is as follows

#include <stdio.h>
#include <malloc.h>

int** malloc2d(int row, int col)
{
    int ** ret = NULL;
    
    if( (row > 0) && (col > 0) )
    {
        int* p = NULL;
        
        ret = (int**)malloc(row * sizeof(int*));
        p = (int*)malloc(row * col * sizeof(int));
        
        if( (ret != NULL) && (p != NULL) )
        {
            int i = 0;
            
            for(i=0; i<row; i++)
            {
                ret[i] = p + i * col;
            }
        }
        else
        {
            free (ret);
            free(p);
            
            ret = NULL;
        }
        
    }
    
    return ret;
}

void free2d(int** p)
{
    if( *p != NULL )
    {
        free(*p);
    }
    
    free(p);
}

intmain()
{
    int** a = malloc2d(3, 3);
    int i = 0;
    int j = 0;
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d, ", a[i][j]);
        }
        
        printf("\n");
    }
    
    free2d(a);
    
    return 0;
}

        We apply for a two-dimensional array a in the main function, print it and release it. Let's see the results

picture.png

        As we wish, the two-dimensional array has been applied. Through the study of multi-dimensional arrays and multi-dimensional pointers, the summary is as follows: 1. Only one-dimensional arrays are supported in C and applications, and its size must be determined as a constant at compile time; 2. The elements in the array can be any type of data , or even another array, which is the essence of multidimensional arrays.


        Welcome to learn C language together , you can add me QQ: 243343083 .

Guess you like

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