Using Structure - C Programming
Program to copy a structure in C.
We can use the below methods to copy one structure to another structure in C language.
• 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.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
floatpercentage;
};
int main()
{
int i;
struct student record1={1, "Mahi", 96.5};
struct student record2, *record3, *ptrl, record4;
printf("Records of STUDENT1 record1 structure \n");
printf(" Id: %d\n Name: %s\n Percentage: %f\n", record1.id, record1.name, record1.percentage);
/* 1sd 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 */
ptrl=&record1;
memcpy(record3, ptrl, 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‒Coping 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 STUDENTI ‒ record structure
Id : 1
Name : Mahi
Percentage : 96.500000
Records of STUDENTI - Direct copy from record1
Id : 1
Name : Mahi
Percentage : 96.500000
Records of STUDENT1 ‒ copied from record1 using memcpy
Id : 1
Name : Mahi
Percentage : 96.500000
Records of STUDENT1‒Coping individual members from recordl
Id : 1
Name : Mahi
Percentage : 96.500000
Computer Programming C Laboratory: Structures C Programs : Tag: C Programming : - C Program to copy a structure in C
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation