Prev Next
C structure can be accessed in 2 ways in a C program. They are,
- Using normal structure variable
- Using pointer variable
Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer variable. You have learnt how to access structure data using normal variable in C – Structure topic. So, we are showing here how to access structure data using pointer variable in below C program.
Example program for C structure using pointer:
In this program, “record1” is normal structure variable and “ptr” is pointer structure variable. As you know, Dot(.) operator is used to access the data using normal structure variable and arrow(->) is used to access data using pointer variable.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> #include <string.h> struct student  {      int id;      char name[30];      float percentage; }; int main()  {      int i;      struct student record1 = {1, "Raju", 90.5};      struct student *ptr;      ptr = &record1;               printf("Records of STUDENT1: \n");          printf("  Id is: %d \n", ptr->id);          printf("  Name is: %s \n", ptr->name);          printf("  Percentage is: %f \n\n", ptr->percentage);      return 0; } | 
Output:
| Records of STUDENT1: Id is: 1 Name is: Raju Percentage is: 90.500000 | 
Example program to copy a structure in C:
There are many methods to copy one structure to another structure in C.
- We can copy using direct assignment of one structure to another structure or
- we can use C inbuilt function “memcpy()” or
- we can copy by individual structure members.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <stdio.h> #include <string.h> struct student  {     int id;     char name[30];     float percentage; }; int main()  {     int i;     struct student record1 = {1, "Raju", 90.5};     struct student record2, *record3, *ptr1, record4;     printf("Records of STUDENT1 - record1 structure \n");     printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",             record1.id, record1.name, record1.percentage);     // 1st method to copy whole structure to another structure     record2=record1;         printf("\nRecords of STUDENT1 - Direct copy from " \            "record1 \n");     printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",              record2.id, record2.name, record2.percentage);     // 2nd method to copy using memcpy function     ptr1 = &record1;      memcpy(record3, ptr1, sizeof(record1));     printf("\nRecords of STUDENT1 - copied from record1 " \            "using memcpy \n");     printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",             record3->id, record3->name, record3->percentage);     // 3rd method to copy by individual members     printf("\nRecords of STUDENT1 - Copied individual " \            "members from record1 \n");     record4.id=record1.id;     strcpy(record4.name, record1.name);     record4.percentage = record1.percentage;     printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",              record4.id, record4.name, record4.percentage);      return 0; } | 
Output:
| Records of STUDENT1 – record1 structure Id : 1 Name : Raju Percentage : 90.500000 Records of STUDENT1 – Direct copy from record1 Id : 1 Name : Raju Percentage : 90.500000 Records of STUDENT1 – copied from record1 using memcpy Id : 1 Name : Raju Percentage : 90.500000 Records of STUDENT1 – Copied individual members from record1 Id : 1 Name : Raju Percentage : 90.500000 | 
Continue on C – Structure within Structure….
Continue on C – Structure Memory Allocation….
Continue on C – Structure Padding….
