Skip to main content
Post Undeleted by R1S8K
edit post
Source Link
R1S8K
  • 283
  • 3
  • 21

I found the answer.Here's my latest work around:

I havewas searching for a work around. I got back to use this configurationcopy a method of casting a function pointer.

And changed my data structure of having 2 nested structs to one that contain everything which should serve my project well.

Here's my work:

task_manager.h

#include<stdio.h>
#include<stdlib.h>///////////////////////////////////////////////////////////////////////////
#include<string.h>// enumerations
#include<stdint.h>typedef enum {NOT_FINISHED,FINISHED,DONE}STATE;
// variables
uint8_ttypedef void (*fun_ptr*f_ptr)(void *args);

uint8_t// read_chars(uint8_tstructs
typedef addressstruct __attribute__((__packed__)) THREAD{
    uint8_t i;
tsk_cnts, tsk_cntr;    uint8_t *array_address;// task information
    iSTATE = address;
 thrd_st;   printf("address of array is 0x%.2x\n",i);           // thread flag states
    returnf_ptr 0x0a;  *tsk_fptr;
}THREAD;

main.c

#include <stdio.h>
uint8_t#include write_chars(uint8_t<stdlib.h>
#include row,<stdint.h>
#include uint8_t"task_manager.h"

const col,unsigned char *str){
PIC[] = {9, 8, uint8_t7, i;
1, 2};

void function1(void){printf("function1\n");}
void function2(uint8_t i){printf("function2\nnumber = strlen(str%d\n",i);}
 void function3(uint8_t a, printf("sizeuint8_t ofb, stringconst isuint8_t %d\n",i*img);{
    printf("%s\n""function3\na#%d b#%d\narray[0] = %d\n",stra,b,*(img+0));}
uint8_t function4(uint8_t i){printf("function4# return\n"); return 0;
i+=1;}

THREAD thread1;

uint8_t fn4_ret;

int main(){ 

    uint8_t// add_val;set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    fun_ptrthread1.tsk_cntr = 0;

    // allocating memory for tasks
    thread1.tsk_fptr = malloc(4*sizeof(f_ptr));

    // store tasks addresses
    thread1.tsk_fptr[0] = (void(*)())read_chars;function1;
    add_valthread1.tsk_fptr[1] = fun_ptr(1void(*);())function2;
    thread1.tsk_fptr[2] = (void(*)())function3;
    fun_ptrthread1.tsk_fptr[3] = (void(*)())write_chars;function4;

    fun_ptr// call the functions
    ((void(*)())thread1.tsk_fptr[1])(9);
    ((void(*)())thread1.tsk_fptr[2])(1,2,PIC);
    fn4_ret = ((uint8_t(*)())thread1.tsk_fptr[3])(1);
    printf("\fn4_ret = %d\n","messaage1"fn4_ret); 


    return 0;
}

I used itIt compiled with two types of functionsme fine, passed what I want and it workedreturned the required data.

I found the answer.

I have to use this configuration of function pointer.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>

uint8_t (*fun_ptr)();

uint8_t read_chars(uint8_t address){
    uint8_t i;
    uint8_t *array_address;
    i = address;
    printf("address of array is 0x%.2x\n",i);
    return 0x0a;
}

uint8_t write_chars(uint8_t row, uint8_t col, char *str){
    uint8_t i;
    i = strlen(str);
    printf("size of string is %d\n",i);
    printf("%s\n",str);
    return 0;
}


int main(){
    uint8_t add_val;


    fun_ptr = (void(*)())read_chars;
    add_val = fun_ptr(1);

    fun_ptr = (void(*)())write_chars;
    fun_ptr(1,1,"messaage1");

    return 0;
}

I used it with two types of functions and it worked.

Here's my latest work around:

I was searching for a work around. I got back to copy a method of casting a function pointer.

And changed my data structure of having 2 nested structs to one that contain everything which should serve my project well.

Here's my work:

task_manager.h

///////////////////////////////////////////////////////////////////////////
// enumerations
typedef enum {NOT_FINISHED,FINISHED,DONE}STATE;
// variables
typedef void (*f_ptr)(void *args);

// structs
typedef struct __attribute__((__packed__)) THREAD{
    uint8_t tsk_cnts, tsk_cntr;     // task information
    STATE   thrd_st;                  // thread flag states
    f_ptr   *tsk_fptr;
}THREAD;

main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "task_manager.h"

const unsigned char PIC[] = {9, 8, 7, 1, 2};

void function1(void){printf("function1\n");}
void function2(uint8_t i){printf("function2\nnumber = %d\n",i);}
void function3(uint8_t a, uint8_t b, const uint8_t *img){
    printf("function3\na#%d b#%d\narray[0] = %d\n",a,b,*(img+0));}
uint8_t function4(uint8_t i){printf("function4# return\n"); return i+=1;}

THREAD thread1;

uint8_t fn4_ret;

int main(){ 

    // set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    thread1.tsk_cntr = 0;

    // allocating memory for tasks
    thread1.tsk_fptr = malloc(4*sizeof(f_ptr));

    // store tasks addresses
    thread1.tsk_fptr[0] = (void(*)())function1;
    thread1.tsk_fptr[1] = (void(*)())function2;
    thread1.tsk_fptr[2] = (void(*)())function3;
    thread1.tsk_fptr[3] = (void(*)())function4;

    // call the functions
    ((void(*)())thread1.tsk_fptr[1])(9);
    ((void(*)())thread1.tsk_fptr[2])(1,2,PIC);
    fn4_ret = ((uint8_t(*)())thread1.tsk_fptr[3])(1);
    printf("\fn4_ret = %d\n",fn4_ret); 


    return 0;
}

It compiled with me fine, passed what I want and returned the required data.

Post Deleted by R1S8K
Post Undeleted by R1S8K
Post Deleted by R1S8K
Source Link
R1S8K
  • 283
  • 3
  • 21

I found the answer.

I have to use this configuration of function pointer.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>

uint8_t (*fun_ptr)();

uint8_t read_chars(uint8_t address){
    uint8_t i;
    uint8_t *array_address;
    i = address;
    printf("address of array is 0x%.2x\n",i);
    return 0x0a;
}

uint8_t write_chars(uint8_t row, uint8_t col, char *str){
    uint8_t i;
    i = strlen(str);
    printf("size of string is %d\n",i);
    printf("%s\n",str);
    return 0;
}


int main(){
    uint8_t add_val;


    fun_ptr = (void(*)())read_chars;
    add_val = fun_ptr(1);

    fun_ptr = (void(*)())write_chars;
    fun_ptr(1,1,"messaage1");

    return 0;
}

I used it with two types of functions and it worked.