function inside struct c 1

function inside struct c

typedef struct client_t client_t, *pno;
struct client_t
{
        pid_t pid;
        char password[TAM_MAX]; // -> 50 chars
        pno next;

        pno (*AddClient)(client_t *);  <-- pointer to function    
};

pno client_t_AddClient(client_t *self) { /* code */ } <-- function

int main()
{

    client_t client;
    client.AddClient = client_t_AddClient; // probably really done in some init fn

    //code ..

    client.AddClient(&client);

}

Here is what the above code is Doing:
1. Define a struct client_t
2. Define a function client_t_AddClient
3. In main, create a client_t object
4. Assign the function pointer to the function
5. Call the function via the function pointer

Similar Posts