Pointer to structure can be defined in the same way as you define pointer to any other variable.
Before accessing the structure member, the structure pointer should be initialized. → operator is used to access the members of a structure using a pointer to that structure.
struct Books *struct_pointer;
Before accessing the structure member, the structure pointer should be initialized. → operator is used to access the members of a structure using a pointer to that structure.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct employee {
char name[50];
int age;
int emp_id;
} ;
void printempdetails(struct employee *emp)
{
printf("emp->name : %s\n", emp->name);
printf("emp->age : %d\n", emp->age);
printf("emp->emp_id : %d\n", emp->emp_id);
}
int main( ) {
struct employee emp1;
struct employee *emp2 = malloc(sizeof(struct employee));
strcpy( emp1.name, "Bob Wright");
emp1.age = 34;
emp1.emp_id = 436587;
strcpy( emp2->name, "John Ray");
emp2->age = 27;
emp2->emp_id = 192837;
printf("Employee 1\n");
printempdetails(&emp1);
printf("Employee 2\n");
printempdetails(emp2);
return 0;
}
The output of the above program would be:
Employee 1
emp->name : Bob Wright
emp->age : 34
emp->emp_id : 436587
Employee 2
emp->name : John Ray
emp->age : 27
emp->emp_id : 192837
Related topics:
Structures in C | Structures and Function Parameters in C | Structure Bit Fields in C | Union in C | Enumeration in C | Typedef in C
List of topics: C Programming
No comments:
Post a Comment