x

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.

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.

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



Like it? Please Spread the word!