C – Structure using Pointer
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.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.
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 |
No comments:
Post a Comment