Structure Assignment in C Programming Language
Notes:
Structure Assignment in C Programming Language:
- How to assign one structure variable to another structure variable
Example Code:
#include <stdio.h>
int main()
{
struct Date
{
int day;
int month;
int year;
};
struct Date date1 = {1,1,2020};
struct Date date2 = {0,0,0};
printf("date1 = %d/%d/%d\n",date1.day,date1.month,date1.year); // date1 = 1/1/2020
printf("date2 = %d/%d/%d\n",date2.day,date2.month,date2.year); // date2 = 0/0/0
printf("\n");
//date2 = date1;
// OR
date2.day = date1.day;
date2.month = date1.month;
date2.year = date1.year;
printf("date1 = %d/%d/%d\n",date1.day,date1.month,date1.year); // date1 = 1/1/2020
printf("date2 = %d/%d/%d\n",date2.day,date2.month,date2.year); // date2 = 1/1/2020
return 0;
}
Note:
If arrays or pointers are members of a structure; then it is recommended to handle assignment of structure members individually. You can create a separate function to handle assignment of particular structure members.