Copying Structure Elements

Following program will illustrate copying structure elements from one structure element to another.


#include <stdio.h>
int main() {
	struct employee {
		char name[10];
		int age;
		float salary;
	struct employee e1 = { "John", 35, 50000.50 };
	struct employee e2, e3;
	/* copying name only from one structure element to another*/
	strcpy(,; // strcpy(destination string, source string)
	e2.age = e1.age;
	e2.salary = e1.salary;
	/* copying all elements at one go */
	e3 = e2;
	printf("\n%s %d %f",, e1.age, e1.salary);
	printf("\n%s %d %f",, e2.age, e2.salary);
	printf("\n%s %d %f",, e3.age, e3.salary);
	return 0;

You may also like...