Wskaźnik do funkcji w strukturze.

0

Witam,

Mam takie pytanie, czy jest możliwe zadeklarowanie takiej struktury?

typedef struct Device_Parameters
{
     int test1;
     int test2;
     ...
     unsigned char ttest;
     void (*functrion)(unsigned int parameter, Device_Parameters* device);
} Device_Parameters;

Jak widać w strukturze znajduje się wskaźnik na funkcję, w której jednym z parametrów jest wskaźnik na tworzoną strukturę.
W chwili obecnej problem rozwiązałem pośrednio:

typedef struct Device_Parameters
{
     int test1;
     int test2;
     ...
     unsigned char ttest;
     void (*functrion)(unsigned int parameter, unsigned char* device);
} Device_Parameters;

i później w wywołaniu funkcji:

Device_Parameters Device;
...
Device.Function(tmp, (unsigned char*) &Device);

i samej funkcji:

void Function1(unsigned int mens, unsigned char* test)
{
     Device_Parameters* device = (Device_Parameters*) test;
     ...
}

Może dałoby się to jakoś uprościć?

Dziękuję i pozdrawiam,
Hiob.

0

Yep, da sie.

struct Device_Parameters;

typedef void (*function)(unsigned int parameter, struct Device_Parameters* device);
 
typedef struct Device_Parameters
{
     int test1;
     int test2;
     unsigned char ttest;
     function func;
} Device_Parameters;
 
void func(unsigned int a, Device_Parameters* dev)
{
    return;
}
 
int main()
{
    Device_Parameters dev = {10, 20, 15, func};
    dev.func(10, &dev);
    
    return 0;
}

http://ideone.com/iLivLR

Swoja droga, calkiem ciekawy pomysl na imitacje klas :)

0

Dziękuję za bardzo szybką odpowiedź.
Niestety w wyniku kompilacji otrzymuje następujący błąd:

Libraries/Device.h warning: 'struct Device_Parameters' declared inside parameter list
Libraries/Device.h warning: its scope is only this definition or declaration, which is probably not what you want

typedef void (*Function1)(unsigned int parameter, struct Device_Parameters* device);
1

Pytanie niewazne, tez moj gcc cos nie trawil tego kodu.

Dodaj na samej gorze cos takiego:

struct Device_Parameters;

To musi dzialac, bo przelecialem wszystkie standardy od C89 ;>

0

Działa! :)

typedef struct Device_Parameters
{
     int test1;
     int test2;
     ...
     unsigned char ttest;
     void (*function)(unsigned int parameter, struct Device_Parameters* device);
} Device_Parameters;

Mógłbys mi wyjaśnić co daje to "struct"?

0

W skrocie to:
prog.c50: error: unknown type name ‘Device_Parameters’

Co znaczy tyle, ze nazwa ta nie jest znana "w srodku" struktury, bo definiujesz ja poza cialem, wiec trzeba dodac struct.

1

Twój problem wynika z tego, że bezmyślnie dajesz "typedef struct" zamiast samego "struct" nie wiedząc, co ten typedef robi.

0

Kolega Azarien chyba jeszcze śpi :)

n0name_l -> wszystko działą i jest ok, chodziło mi o tą linię i co w tym przypadku daje to "struct":

void (*function)(unsigned int parameter, struct Device_Parameters* device);
0

Mój błąd. Zwracam honor! :)

Dziękuję i pozdrawiam!
Hiob.

1 użytkowników online, w tym zalogowanych: 0, gości: 1