LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Structure in C (http://www.linuxquestions.org/questions/programming-9/structure-in-c-673022/)

divya_flora 09-29-2008 02:50 AM

Structure in C
 
Hi Everyone....
I have a structure:
typedef struct
{
unsigned char command;
unsigned char data_length;
char data[MAX_DATA_LENGTH];
}PACKET;

My problem is that,I want to use a function here...
When i assign values to these variables they are as:
COMMAND,20,'S'...........
I want to call a function here..or assign a function value here........
as,
COMMAND,20,make().........

is it possible to do so...

Thanks ...
Divya

jf.argentino 09-29-2008 03:46 AM

Hi,
Yes it's possible, you have to use a pointer on function, something like

Code:

struct Packet {
    int (*do_command) (arg);
    unsigned char data_length;
    char data [MAX_DATA_LENGTH];
};

int my_command (arg) {
  /* Do what you want */
}

int main (void) {
  struct Packet packet;
  packet.do_command = my_command;
  /* ... */
}



All times are GMT -5. The time now is 11:34 PM.