A structure in C Language is a collection of variables of heterogeneous or various data types that can be manipulated together.
Using structure makes code easier to understand and maintain. In this tutorial article section lets learn different ways to copy structure in C programming language.
Copying: refers to creating new object by copying original object or reuse all or some of the data .In C programming copying is the process of making an exact copy of the variable or object , i.e., both the variables or object must have the same value.
When we create a copy of something, it is expected to be an independent object and similar to the thing you copied. The definition can vary in programming . So, we are also going to study in depth about deep and shallow copy.
Copy and Compare Structure in C programming.
Copy Structure:
From the given figure we can say that the Structure can be copied in different ways.
1.Copy structure attribute by attribute into another structure.
2.Deep Copy of struct in C.
3.Shallow Copy of struct in C.
Before to proceed first of all let learn what is Deep Copy and Shallow copy in C language.
Deep Copy: Copying the specific content of the object is know as deep copy. Deep copy creates a new memory address in the computer to store the copied object. Changes to the original source data do not affect the copied or replicated data.
Shallow Copy:The copy that you get from an assignment statement with a structure variable is a bit-for-bit copy, also called a shallow copy.
1. Copy attribute by attribute of struct into another struct.
This approach of copying structure is veary simple. In this aproach we copies structure attribute by attribute into another structure.
This approach of copying struture become more difficult to manage and typing.
Following is the program to demonstrate copying structure attribute by attribute.
/*copy structure attribute by attribute into another structure*/
#include <stdio.h>
#include <string.h>
typedef struct employee {
int empno ;
char name [ 40 ];
char gender[10];
float salary;
}Emp;
void print_emp(Emp *e1)
{
printf("\n(Empno: %d, Ename: %s, Gender: %s, Salary: %.2f )",
e1->empno,e1->name,e1->gender,e1->salary);
}
int main ( )
{
Emp user={100,"Kailas","Male",45000},user1;
/* copy structure atteibute by attribute */
user1.empno=user.empno;
strncpy(user1.name,user.name,sizeof(user.name));
strncpy(user1.gender,user.gender,sizeof(user.gender));
user1.salary=user.salary;
printf("\n Employee: ");
print_emp(&user);
printf("\n Employee Copy: ");
print_emp(&user);
return (0);
}
Output:
Employee: (Empno:100, Ename:Kailas, Gender:Male,Salary:45000)
Employee copy: (Empno:100, Ename:Kailas, Gender:Male,Salary:45000)
3.Shallow copy.
/**
3.Shallow Copy of struct in C **/
#include <stdlib.h>
#include<stdio.h>
#include <string.h>
typedef struct student
{
char *name;
int sid;
float per;
}student;
int main()
{
student s1;
s1.name = (char*)malloc(25);
strcpy(s1.name, "Sachin");
s1.sid = 22;
s1.per=89.00;
student s2 = s1;
printf("\n Student Info\n");
printf(" %d %s %.2f",s2.sid, s2.name, s2.per);
if (s1.name != NULL)
{
free(s1.name);
s1.name = NULL;
}
return 0;
}
Output:
Student Info
22 Sachin 89.00
2. Deep copy
#include <string.h>
#include <stdio.h>
typedef struct Employee
{
char *name;
int empno;
}emp;
int main(int argc, char *argv[])
{
emp e1;
emp e2;
e1.name = (char *)malloc(10);
e1.empno = 20;
strcpy(e1.name, "Raviraj");
printf("Empno: %d name: %s, \n", e1.empno, e1.name);
e2 = e1;
strcpy(e2.name, e1.name);
printf("Empno: %d e2->name: %s\n",e2.empno, e2.name);
free(e1.name);
free(e2.name);
return 0;
}
Output:
Empno: 20 name:Raviraj
Empno: 20 e2->name:Raviraj
Compare structure in C programming.
1.Both Structure Variable have the Same Value.
This is the safe way to compare the structure members by member for the equality.
/*Compare the the structure variable when Both have the same value*/
#include <stdio.h>
struct person
{
int pid;
char G;
};
int main()
{
struct person p1, p2;
/*Assigning value to the p1*/
p1.pid = 1;
p1.G ='M';
/*Assigning value to the p2*/
p2.pid = 1;
p2.G ='M';
//Comparing the structure variable
if((p1.pid == p2.pid) && (p1.G == p2.G))
{
printf("Struct variables are equal\n");
}
return 0;
}
Output: Struct variables are equal
2.Both Structure Variable don't have the Same Value.
This is the another way to compare the structure members.
/*Compare the the structure variable when Both structure don't have the same value*/
#include <stdio.h>
struct person
{
int pid;
char G;
};
int main()
{
struct person p1, p2;
/*Assigning value to the p1*/
p1.pid = 21;
p1.G ='F';
/*Assigning value to the p2*/
p2.pid = 11;
p2.G ='M';
//Comparing the structure variable
if((p1.pid == p2.pid) && (p1.G == p2.G))
{
printf("Struct variables are equal\n");
}
else
{
printf("Struct variables are not equal\n");
}
return 0;
}
Output:
Struct variables are not equal.
Previous Topic:-->>Structure Initialization in C || Next topic:-->>Array of Structure.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ