0

I'm getting an error when I try to use a C function pointer inside of a struct (the function just pops the first element off of a linked list and returns the element):

main.c: In function ‘simulation’: main.c:279: error: incompatible types when assigning to type ‘struct PROCESS *’ from type ‘PROCESS’

My code is this:

typedef struct PROCESS {
    struct PS_TABLE *tbl_ref;
    int pid;
    int time_in_prev_state;
    int state_ts;
    int static_prio;
    int dynamic_prio;
    int cpu_rem;            
    struct PROCESS *next;
} PROCESS;

typedef void (*Add) (PROCESS *head, PROCESS *new_ps);
typedef PROCESS (*Get) (PROCESS *head);

typedef struct SCHEDULER {
    int quantum;
    sched_t sch_alg;
    Add add_process;
    Get get_next_process;
} SCHEDULER;

PROCESS *fcfs_get_next_proc(PROCESS **head) {     //POP
    PROCESS *tmp = head;
    head = tmp->next;
    tmp->next = NULL;
    return tmp;
}

SCHEDULER *scheduler_obj = malloc(sizeof(SCHEDULER));
scheduler_obj->get_next_process = fcfs_get_next_proc;

PROCESS *RUNNING_PROCESS = NULL;
RUNNING_PROCESS = scheduler->get_next_process(head_proc);

Any help would be really appreciated!

3
  • 3
    Try typedef PROCESS* (*Get) (PROCESS *head); Commented Jul 5, 2018 at 14:47
  • 1
    Your function PROCESS *fcfs_get_next_proc(PROCESS **head) return pointer, but your fucntion pointer prototype return simple PROCESS. As @Ctx suggest, try his solution. Commented Jul 5, 2018 at 14:49
  • PROCESS *tmp = head; head = tmp->next; should be PROCESS *tmp = *head; *head = tmp->next; Commented Jul 5, 2018 at 14:58

1 Answer 1

1

Your next struct field is a struct PROCESS *, and your function returns a PROCESS. Make them the same type and it will work.

typedef struct PROCESS *(*Get) (PROCESS *head);
typedef PROCESS *(*Get) (PROCESS *head);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.