How do I call a function with a functionpointer from a struct?

I am trying to learn c but am stuck with this problem. I want to create a linked list with this structure. I am trying to compose a list with various data types including various led_blinking functions. The problem is that I cannot call the function with the function pointer added to the structure in the list. Can anyone help me with this problem? What am I doing wrong?

#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");
    }
}

      

Code developed for Tiva by TI Lauchpad. PLease Help.

+3


source to share


1 answer


You need a function pointer declaration to match the prototype of the functions to be assigned. You can deal with this problem with the following changes.

Modify your function prototypes as follows:

void ledRedTask(void) { ... }

void ledGreenTask(void) { ... }

etc...

      

Then you declare your function pointer to match the prototypes of those functions:

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

      



You can set the pointer i.e.

 ptr->functionTask = ledTask;

      

And you can call it like this:

ptr->functionTask();

      

+2


source







All Articles