C – Structure using Pointer

      Prev                                                                                              Next

C structure can be accessed in 2 ways in a C program. They are,

    1. Using normal structure variable
    2. 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.

#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 nn", 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.

    1. We can copy using direct assignment of one structure to another structure or
    2. we can use C inbuilt function “memcpy()” or
    3. we can copy by individual structure members.
#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 : %sn  Percentage : %fn",
            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 : %sn  Percentage : %fn", 
            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 : %sn  Percentage : %fn", 
           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 : %sn  Percentage : %fn", 
            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….

      Prev                                                                                              Next