C array pointer and pointer array (29)

        Let's take a look at the legendary pointer array and array pointer today. In the C language, arrays have their own specific types. So what is the type of the array? It is determined by the element type and the size of the array. For example, the type of int array[5] is int[5] .

        In the C language, we can rename the array type through typedef , the format is: typedef type(name)[size] ; where the array type can be expressed like this: typedef int(AINT5)[5] ; typedef float(AFLOAT10)[10] ; Then the array definition can be expressed as: AINT5 iarray; AFLOAT10 farray ;

        The array pointer is used to point to an array . The array name is the starting address of the first element of the array but not the actual address of the array; the starting address of the array can be obtained by taking the address character & acting on the array name, and the array pointer can be defined by the array type. : ArrayType* pointer ; can also be defined directly: type(*pointer)[n] ; where pointer is the variable name of the array pointer, type is the element type of the pointed array, and n is the size of the pointed array .

        Let's analyze it through the sample code, the code is as follows

#include <stdio.h>

typedef int(AINT5)[5];
typedef float(AFLOAT10)[10];
typedef char(ACHAR9)[9];

intmain()
{
    AINT5 a1;
    float fArray[10];
    AFLOAT10* pf = &fArray;
    ACHAR9 cArray;
    
    char(*pc)[9] = &cArray;
    char(*pcw)[4] = cArray;
    
    int i = 0;
    
    printf("%d, %d\n", sizeof(AINT5), sizeof(a1));
    
    for(i=0; i<10; i++)
    {
        (*pf)[i] = i;
    }
    
    for(i=0; i<10; i++)
    {
        printf("%f\n", fArray[i]);
    }
    
    printf("%p, %p, %p\n", &cArray, pc+1, pcw+1);
    
    return 0;
}

        Let's analyze this code. Three types of arrays are defined in lines 3-5, and a pointer of type char[9] is defined in line 14 and points to cArray, because the type of cArray is also char[9] , so this sentence will not go wrong. A pointer of type char[4] is defined on line 10, but initialized with cArry, which causes problems. Because the types are different, an error will be reported. In line 23, pf is a pointer to the array fArray, adding * in this block is equivalent to taking the value in the array, then the meaning of this sentence is to assign a value to the array fArray. On line 31, the first one printed is the address of the array cArray, pc + 1 is equivalent to pointer arithmetic, that is (unsigned int)pc + sizeof(*pc) ==> (unsigned int)pc + 1 * 9 ; pcw + 1 ==> (unsigend int)pcw + sizeof(pcw) ==> (unsigned int)pcw + 1 * 4; let's see the compilation result

picture.png

        We see that the program just reported a warning on line 15, but the results of the last three lines are as we analyzed. As professional programmers, we must treat every warning as an error, because a warning also means that the program may not run normally, and no one knows what will happen.

        Let's talk about the pointer array again. In fact, the pointer array is an ordinary array, and each element in it is a pointer . Definition of pointer array: type* pArray[n] ; where type* is the type of each element in the array, pArray is the name of the array, and n is the size of the array .

        Let's analyze it with sample code, the code is as follows

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

#define DIM(a) (sizeof(a)/sizeof(*a))

int lookup_keyword(const char* key, const char* table[], const int size)
{
    int ret = -1;
    
    int i = 0;
    
    for(i=0; i<size; i++)
    {
        if( strcmp(key, table[i]) == 0 )
        {
            ret = i;
            break;
        }
    }
    
    return ret;
}

intmain()
{
    const char* keyword[] = {
            "do",
            "for",
            "if",
            "register",
            "return",
            "switch",
            "while",
            "case",
            "static"
    };
    
    printf("%d\n", lookup_keyword("return", keyword, DIM(keyword)));
    printf("%d\n", lookup_keyword("main", keyword, DIM(keyword)));
    
    return 0;
}

        The function of our function is very simple, that is, to find a string in an array of pointers, and return its number if found. If not found, return -1, let's see the compilation result

picture.png

        We see that the function does implement this functionality. Through the study of array pointers and pointer arrays, we can summarize as follows: 1. The type of the array is determined by the element type and the size of the array; 2. The array pointer is a pointer that points to an array of the corresponding type; the pointer array is an array, in which each All elements are pointers; 3. Array pointers follow pointer arithmetic, and pointer arrays have various characteristics of C language 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=324608160&siteId=291194637