首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C中通过结构使用函数指针?

如何在C中通过结构使用函数指针?
EN

Stack Overflow用户
提问于 2019-04-12 21:40:51
回答 2查看 69关注 0票数 0

我正在尝试使用函数指针和结构打印时间。它不会产生任何错误。它首先工作,但后来"Test.exe停止运行!“。

我的文件是: Random.c Random.h,Randomness.c Randomness.h,Test.c

Random.h

代码语言:javascript
复制
struct RANDOM {
    char* date;
    char* (*Date) (struct RANDOM*);
    void (*Write) (struct RANDOM*);
};
typedef struct RANDOM* Random;

Random CreateRandom();
char* DateOfNow(const Random);
void WriteDate(const Random);

Random.c

代码语言:javascript
复制
char* BringTime(){
    char* buff = malloc(sizeof(char)*100);
    time_t now = time(0);
    strftime(buff, 100, "%Y-%m-%d %H:%M",localtime(&now));

    return buff;
}

Random CreateRandom(){
    Random this;
    this = (Random) malloc(sizeof(struct RANDOM));  
    this->date = BringTime();

    return this;
}

char* DateOfNow(const Random this){
     return this->date;
}

void WriteDate(const Random this){
    printf("\n\n Date is: %s", this->date);
}

Randomness.h

代码语言:javascript
复制
struct RANDOMNESS{
    Random super;
};

typedef struct RANDOMNESS* Randomness;

Randomness CreateRandomness();

Randomness.c

代码语言:javascript
复制
Randomness CreateRandomness(){
    Randomness this;
    this = (Randomness)malloc(sizeof(struct RANDOMNESS));
    this->super = CreateRandom();

    return this;
}

Test.c

代码语言:javascript
复制
int main() {

    Randomness rnd = CreateRandomness();
    printf("works till here");
    rnd->super->Write(rnd->super);
}

输出为: works is here

在该输出之后,它将停止运行"Test.exe stops“。

我试过printf("%p", rnd->super),它给了我地址。因此,Write(rnd->super)函数可能存在问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-12 22:21:57

您必须将函数指针分配给结构中的成员字段:

代码语言:javascript
复制
Random CreateRandom(){
    Random this;
    this = (Random) malloc(sizeof(struct RANDOM));  
    this->date = BringTime();
    // assign function pointer to actual functions
    this->Date = &DateOfNow; 
    this->Write = &WriteDate;

    return this;
}

可以肯定的是,DateOfNowWriteDate的原型应该在CreateRandom定义之前可用。

注意:您可以编写this->Date = DateOfNow; (没有&,因为带有函数标识符的&是一个剩余)。

票数 0
EN

Stack Overflow用户

发布于 2019-04-12 22:23:36

您的create函数不完整:

代码语言:javascript
复制
Random CreateRandom(){
    Random this;
    this = (Random) malloc(sizeof(struct RANDOM));  
    // Content of the memory is undefined!
    this->date = BringTime();
    // What about Write() and Date()? <<<======= ERROR IS HERE
    return this;
}

...

Randomness CreateRandomness(){
    Randomness this;
    this = (Randomness)malloc(sizeof(struct RANDOMNESS));
    this->super = CreateRandom();

    return this;
}

...

int main() {

    Randomness rnd = CreateRandomness();
    printf("works till here");
    rnd->super->Write(rnd->super); // << using unspecified values is undefined behaviour.
}

您为date赋值,但不为DateWrite赋值。这意味着您在rnd->super中有一些有用的值,但是rnd->super->Write的内容是未定义的。

如果要将Create*函数用作一种构造函数,还必须正确设置函数指针。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55653029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档