-->

Copying and Comparing Structure Variables

Copying and Comparing Structure Variables
Copying and Comparing Structure Variables
Two variables of the same structure type can be copied the same way as ordinary variables.
If e1 and e2 belong to the same type, then the following statement is valid. e1 = e2, and e2 = e1;
However, the statements that are shown here:
e1 < e2; and e1 != e2; are not permitted.
C language doesn't permit any logical operations on structure variables.
We can compare two structure variables but comparison of members of a structure can only be done individually.


Write a program to illustate the comparison of structure variables
#include<stdio.h>
#include<conio.h>
struct class
{
int number; char name[20];
float marks;
};
main()
{
int x;
//Declaring and initializing structures of 'class' type
struct class student2 = {2, "gita", 78.00};
struct class student3;
student3 = student2; // Copying student2 to student3
if ((student3.number = student2.number) && (student3.marks = student2.marks)) // verifying results of copy
{
printf("\n student2 and student3 are equal");
printf("%d %s %f\n", student3.number, student3.name, student3.marks);
}
else
printf("\n student2 and student3 are different");
}















student3 = student2; à This will copy values of members of student2 to corresponding members of student3

Output:
student2 and student 3 are equal.



Two variables of the same structure type can be copied the same way as ordinary variables.
If e1 and e2 belong to the same type, then the following statement is valid.
e1 = e2; and e2 = e1;  
However, the statements that are shown here:
e1 < e2; and e1 != e2; are not permitted.
C language does not permit any logical operations on structure variables.
We can compare two structure variables but comparison of members of a structure can only be done individually.



Related Posts

Subscribe Our Newsletter