简体   繁体   English

我不明白这段代码中的指针是如何工作的?

[英]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?我的意思是alloc_MY_CAR()返回一些数组以及&如何工作,以便newTab->pFunFree = &free_MY_CAR看到newTab->pDat返回的这个数组?

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 .请注意,函数free_MY_CAR有一个void*类型的参数,一个指向“void”类型的指针(这是一个指向某事物的 C 习语,但不告诉所指向事物的类型),它所做的第一件事是将该指针重新解释为指向MY_CAR的指针。

So the function is probably intended to be called like this:所以该函数可能打算这样调用:

newTab->pFunFree(newTab->pDat); 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 .也就是说,函数“知道”什么指针由alloc_MY_CAR()返回并存储在newTab->pDat是程序员明确地告诉函数什么指针存储在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 .在上面对pFunFree的调用中, newTab可能已由问题中显示的case 0代码初始化,但可能还有另一种情况使用alloc_MY_JOB()&free_MY_JOB&print_MY_JOB ,其中MY_JOB函数分配/释放/打印一种&free_MY_CAR alloc_MY_CAR()&free_MY_CAR&print_MY_CAR使用的数据结构完全不同的数据结构。 Then if you call那么如果你打电话

newTab->pFunPrint(newTab->pDat); 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() ;当我们编写代码时,我们可能无法预测它是否会打印由alloc_MY_CAR()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 .但我们可以预测它会打印关于你的汽车、你的工作或从数据文件中读取并存储在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. C++ 的卖点之一是使人们能够比这更容易地编写多态对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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