如何从结构中调用带有functionpointer的函数?

时间:2017-03-27 21:18:40

标签: c

我正在努力学习c但是我遇到了这个问题。我想用这个结构创建一个链表。我试图用不同类型的数据制作一个列表,包括不同的led_blinking函数。问题是我无法使用添加到列表中的struct的函数指针来调用该函数。有人可以帮我解决这个问题吗?我做错了什么?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <tm4c123gh6pm.h>
#include <sysctl.h>
#include <gpio.h>

void ledRedTask(){
    GPIO_PORTF_DATA_R = RED;
    delay();
}

void ledGreenTask(){
    GPIO_PORTF_DATA_R = GREEN;
    delay();
}

void ledBlueTask(){
    GPIO_PORTF_DATA_R = BLUE;
    delay();
}

void ledYellowTask(){
    GPIO_PORTF_DATA_R = YELLOW;
    delay();
}

void ledPinkTask(){
    GPIO_PORTF_DATA_R = PINK;
    delay();
}

void ledAquaTask(){
    GPIO_PORTF_DATA_R = AQUA;
    delay();
}

struct taskStruct{
    int taskNumber;
    int taskTime;
    void (*functionTask)(void *);
    struct taskStruct *next;
};

struct taskStruct *head = NULL;
struct taskStruct *curr = NULL;

struct taskStruct* create_list(int taskNumber, void(led_task)(), int delay)
{
    struct taskStruct* ptr = (struct taskStruct*)malloc(sizeof(struct                                                           taskStruct));
    if(NULL == ptr)
    {
        return NULL;
    }
    ptr->taskNumber = taskNumber;
    ptr->taskTime = delay;
    ptr->functionTask = led_task;
    ptr->next = NULL;

    head = curr = ptr;
    return ptr;
}

struct taskStruct* add_to_list(int taskNumber, bool add_to_end, void(ledTask)(), int ticks_delay)
{
    //If list is not created yet, create list
    if(NULL == head)
    {
        return (create_list(taskNumber, ledTask, ticks_delay));
    }

    struct taskStruct *ptr = (struct taskStruct*)malloc(sizeof(struct taskStruct));

    ptr->taskNumber = taskNumber;
    ptr->taskTime = ticks_delay;
    ptr->functionTask = ledTask;
    ptr->next = NULL;

    if(add_to_end)
    {
        curr->next = ptr;
        curr = ptr;
    }
    else
    {
        ptr->next = head;
        head = ptr;
    }
    return ptr;
}

void printTaskList(void)
{
    struct taskStruct *ptr = head;

    while(ptr != NULL)
    {
        ptr = ptr->next;
    }
    return;
}

struct taskStruct* search_in_list(int taskNumber, struct taskStruct **prev)
{
    struct taskStruct *ptr = head;
    struct taskStruct *tmp = NULL;
    bool found = false;

    while(ptr != NULL)
    {
        if(ptr->taskNumber == taskNumber)
        {
            found = true;
            break;
        }
        else
        {
            tmp = ptr;
            ptr = ptr->next;
        }
    }

    if(true == found)
    {
        if(prev)
            *prev = tmp;
        return ptr;
    }
    else
    {
        return NULL;
    }
}

void main(){
    initSystem();

    struct taskStruct* ptr = NULL;

    printTaskList();

    //add new tasks with tasknumber, adding to end of list?, which color should be used?, for how long?
    add_to_list(0, true, ledRedTask, 100);
    add_to_list(1, true, ledRedTask, 100);
    add_to_list(2, true, ledRedTask, 100);
    //add_to_list(2, true, green, 20);
    //add_to_list(3, true, blue, 5000);

    printTaskList();

    while(1){

        ptr = search_in_list(1, NULL);


        __asm(" WFI");
    }
}

该代码是为Tiva TI Lauchpad开发的。请帮助。

1 个答案:

答案 0 :(得分:2)

您需要声明函数指针匹配它将被分配的函数的原型。您可以通过以下修改来解决此问题。

更改您的函数原型:

void ledRedTask(void) { ... }

void ledGreenTask(void) { ... }

etc...

然后以匹配这些函数的原型的方式声明您的函数指针:

void (*functionTask)(void); // <-- NOT void (*functionTask)(void *);

您可以设置指针,即

 ptr->functionTask = ledTask;

你可以像这样调用它:

ptr->functionTask();