How to interpret complex functions?

Int *(*(* PFN)(int *))[10]; What function is this? This article will guide you to understand this complex function easily.

In C, there is an explicit resolution rule for complex functions:

The PFN pointer is a pointer to a variable name. The PFN pointer is a pointer to a variable name and a pointer to a variable name. When the contents of the parentheses are parsed, the parentheses are removed, and the process is repeated until the expression is parsed. In fact, complex functions are inseparable from pointer functions, function Pointers, pointer arrays, and array Pointers.

It’s not hard to decipher complex functions, so let’s start with the simplest:

Func is a pointer to a function (int) that returns an int

int (*func)(int);
Copy the code

As shown in the derivation diagram below:

Let’s look at a slightly more complicated function:

Fun2 is an array whose elements hold Pointers to a function (int *) whose return value is int

int (*fun2[5]) (int *p);
Copy the code

As shown in the schematic diagram below:

Here’s another code:

Funn3 is a pointer to an array with five elements [5], so fun3 is just trying to keep his funn3, whose element type is a pointer to a function (int *p) that returns an int

int (*(*func3)[5]) (int *p);
Copy the code

Just look at the diagram below to see:

Let’s look at another complicated function:

Func4 is a pointer to a function (int *) that returns a pointer to an array [5] that contains elements of type int

int (*(*func4)(int *p))[5];
Copy the code

Let’s go back to that complicated function we had at the beginning

PFN is a pointer to a function (int *). The return value is a pointer to an array [10]. The return value is an array pointer to the elements of the array containing int *


    int *(*(*pfn)(int*))10];
Copy the code

Through the above from easy to difficult to interpret the complex function, in the future, in the face of complex functions can be easily interpreted, according to the right and left rule for interpretation.

One more thing to note in a function is that a function cannot return an array, and the elements in an array cannot be function types. There are Pointers to the function’s return array type and Pointers to array elements to the function type.

    //C can't return an array directly. We need a pointer to an array
// int (*func6)(int)[5];
    int (*(*func6)(int))5];

    Func5 is an array of five elements, all of which are functions. This is illegal because the elements of an array must be of the same type and each element must occupy the same space
    // Functions usually take up different space, and are usually referred to by Pointers.
// int func5[5](void);
    int (*func5[5]) (void);// The pointer array element points to a function
Copy the code

IO file operation

C language how to operate files, this is also one of the technical points to master.

  1. Create a file

The files created by C are in the same directory as the.exe file, because C files are compiled ->.o files -> compile the executable.exe file

C ->.o ->.exe
int main1(a) {
    FILE *p = fopen("./test/m.txt"."w");// Open the file
    //r is read-only, w is written
    if (p) {
        fclose(p);// Close the file
        // then p is the dangling pointer
        p = NULL;
        // File exists
        printf("File exists");
    } else {
        // File does not exist
        printf("File does not exist");
    }
    return 0;
}
Copy the code
  1. Read files The read files must be in the same directory as the. Exe file
/** * read the file */
int main2(a) {
    / / read the file
    FILE *p = fopen("./test/m.txt"."r");
    if (p) {
        while (1) {
            char c = fgetc(p);
            EOF = EOF; EOF = EOF
            if (c == EOF) {
                break;
            }
            printf("c:%c\n", c);//c:a
        }
    }
    fclose(p);
    p = NULL;
    return 0;
}
Copy the code
  1. Write file operation
/** * write file */
int main3(a) {
    FILE *p = fopen("./test/m.txt"."w");// Open the file
    if (p) {
        fputc('b', p);// All overwrite
        fclose(p);
        p = NULL;
    }
    return 0;
}
Copy the code
  1. The operation of copying a file
/** * copy file */
int main(a) {
    FILE *p = fopen("./test/write.txt"."w");// Copy to a new file
    FILE *p1 = fopen("./test/m.txt"."r");// Read the original file
    if (p == NULL || p1 == NULL) {
        return - 1;
    }
    while (1) {
        char c = getc(p1);
        if (c == EOF) {
            break;
        }
        fputc(c, p);
    }
    fclose(p);
    fclose(p1);
    p = NULL;
    p1 = NULL;
    return 0;
}
Copy the code
  1. Parse file content key!!

When using C to do audio and video development often have to parse audio and video files, here needs to master how to parse files, first to parse some simple content.

For example, if the following information exists in the file, parse it and calculate the result

1+2=
42 -=
1*3=
6/2=
Copy the code

In this case, fgets is used to read a line of data and sscanf is used to format the data. Generally, EOF is used to judge the reading of a single character. Feof () is used to judge whether the last line is read or not

int acc(int a, char b,int c){
    int result = 0;
    switch (b) {
        case '+':
            result = a + c;
            break;
        case The '-':
            result = a - c;
            break;
        case The '*':
            result = a * c;
            break;
        case '/':
            result = a / c;
            break;
    }
    return result;
}

int main(a) {
    FILE *p = fopen("./test/m1.txt"."r");
    if (p) {
        while (1) {
            char buffer[100] = {0};// Define 100 char, all set to 0, recommended for dirty data in memory
            // Reads a line of gets
            fgets(buffer, sizeof(buffer), p);// Read a row of data into buffer
            int a = 0;
            char b = 0;
            int c = 0;
            // Format read
            sscanf(buffer, "%d%c%d", &a, &b, &c);
            printf("a:%d b:%c c:%d = %d\n",a,b,c, acc(a,b,c));

            Check whether the end of the file is read by feof
            if (feof(p)) {
                break;
            }
        }
    }
    fclose(p);
    p=NULL;
    return 0;
}
Copy the code

The result is as follows:

The following code is used to read the video file:

#define BLOCK_SIZE (1024 * 64)

int main(int argc,char** argv){
    //argc argument length size
    Argv [0] is occupied by the system
    //rb reads binary wb writes binary
    printf("argc:%d agrv1:%s,agrv2:%s\n",argc,argv[1],argv[2]);//argc:1 agrv:(null)
    FILE *pr1 = fopen(argv[1]."rb");
    if(pr1 == NULL) return 0;
    FILE *pr2 = fopen(argv[2]."wb");
    if(pr2 == NULL) return 0;

    // Set the buffer first
    struct stat st = {0};
    // Get the size of the file
    stat(argv[1],&st);
    int size = st.st_size;// How to set this size inside cannot say video 100M allocate 100M memory must be judged
    printf("size:%d\n",size);
    if(size > BLOCK_SIZE){// If the size is larger than 64KB, the file is read in chunks
        size = BLOCK_SIZE;
    }
    char *buf = calloc(1,size);//calloc declares a memory space and sets it to the initial value of memory malloc declares a memory space will not be set to 0

    unsigned int index = 0;//auto Indicates an unsigned positive number

    while(! feof(pr1)){// Return the number of files that were finally read
        int res = fread(buf,1,size,pr1);// Pr1 files read 1 size, read into buffer
        // Write to the target file
        fwrite(buf,1,res,pr2);
        index ++;
    }
    free(buf);
    buf = NULL;
    fclose(pr1);
    fclose(pr2);
    pr1 = NULL;
    pr2 = NULL;
    return 0;
}
Copy the code

Enter the following command in the exe directory:cbase.exe ./test/input.flv ./test/output.flvIn the test file, store the input. FLV file, which is the generated video file.