Function inside structure in C code example
Example 1: structure and function in c
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
return 0;
}
void func(struct student *record)
{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
Example 2: function inside struct c
typedef struct client_t client_t, *pno;
struct client_t
{
pid_t pid;
char password[TAM_MAX];
pno next;
pno (*AddClient)(client_t *); <-- pointer to function
};
pno client_t_AddClient(client_t *self) { } <-- function
int main()
{
client_t client;
client.AddClient = client_t_AddClient;
client.AddClient(&client);
}
Example 3: structure and function in c
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}