简体   繁体   中英

I don't understand how pointers work in this code?

I don't understand this part of the code below. I mean alloc_MY_CAR() returns some array and how does & work so that newTab->pFunFree = &free_MY_CAR sees this array which newTab->pDat returns?

I don't understand pointers well. I only know that & store address of variable and * is a pointer or a value of the variable. Could anyone guide me on how to use it properly and how does it work? I'm a beginner, so don't be so hard on me.

Thanks in advance!

#pragma once
struct MY_CAR {
    char *model;
    int year;
};


void print_MY_CAR(void* pdata);
void free_MY_CAR(void *pdata);

MY_CAR* alloc_MY_CAR();



    switch (typ) {
        case 0:
            newTab->pDat = alloc_MY_CAR();
            newTab->pFunFree = &free_MY_CAR;
            newTab->pFunPrint = &print_MY_CAR;
            break;
    }
    MY_CAR* alloc_MY_CAR() {
        MY_CAR* tab = (MY_CAR*)calloc(1, sizeof(MY_CAR));
        if (!tab) {
            exit(0);
        }
        else {
            char model[125];
            printf("Podaj model: ");
            scanf("%s", model);
            tab->model = (char*)calloc(strlen(model) + 1, sizeof(char));
            strcpy(tab->model, model);
            printf("Podaj rok: ");
            scanf_s("%d", &tab->year);
        }
        return tab;
    }
void free_MY_CAR(void *pdata) {
    MY_CAR* car = (MY_CAR*)pdata;
    if (!car || !car->model) return ;
    free(car->model);
    free(car);
}

Notice that the function free_MY_CAR has an argument of type void* , a pointer to a "void" type (which is a C idiom for a pointer to something without telling the type of the thing pointed to), and the first thing it does is to reinterpret that pointer as a pointer to a MY_CAR .

So the function is probably intended to be called like this:

newTab->pFunFree(newTab->pDat);

That is, the way the functions "know" what pointer was returned by alloc_MY_CAR() and stored in newTab->pDat is that the programmer explicitly tells the functions what pointer is stored in newTab->pDat .

The advantage of doing such things is that it allows some code to do some operation on a data structure without necessarily having to know what kind of data structure it will actually operate on when the program actually runs. In the call to pFunFree above, newTab could have been initialized by the case 0 code shown in the question, but there could be another case that initializes it with alloc_MY_JOB() , &free_MY_JOB , and &print_MY_JOB , where the MY_JOB functions allocate/free/print a data structure that is quite different from the data structure used by alloc_MY_CAR() , &free_MY_CAR , and &print_MY_CAR . Then if you call

newTab->pFunPrint(newTab->pDat);

we might not be able to predict when we write the code whether it will print the contents of a data structure created by alloc_MY_CAR() or by alloc_MY_JOB() ; but we can predict that it will print the detailed information it has about your car, or your job, or whatever was read from the data file and stored in newTab .

The property that we can make a function call that uses a data structure in a way appropriate to that data structure, without having to know when we write the code what the type of data structure will be, is called polymorphism .

This is a cumbersome idiom and there are lots of ways to get it wrong. One of the selling points of C++ is to enable people to write polymorphic objects more easily than this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM