Comparison of structure variables In C

Two variables of the same structure type can be compared the same wat as ordinary variables. If student1 and student2 belong to the same structure, then the following operations are valid.


Program – Comparison of structure

struct class 
{
    int number;
    char name [20];
    float marks;
};
void main ()
{
    int x; 
    static struct class student1 = {123, “Ram”, 82.60};
    static struct class student2 = {123, “Laxman”, 95.50};
    struct class student3;
    student 3= student2;
    x = ((student 3. number == student2. number) && (student3.marks == student2. marks))? 1:0; 
    if (x == 1)
    {
        printf (“\nstudent2 and student3 are same\n\n”);
        printf (“%d%s%f\n”, student3. number, student3. name,
        student3. marks);
    } 
    else 
        printf (“\nstudent2 and student3 are different \n\n”);
}

Output

student2 and student3 are same
234 Laxman 95.500000


Working with structures

The structure can be used as a whole in the following ways

  • • the entire structure can be copied to another structure of same type by using assignment operator
  • • the address of the structure can be used by using ‘&’ operator
  • • the entire structure can be passed as an argument to a function
  • • function can also return a structure

If the member of a structure is a basic data type (i.e., int, float, char, double), it can do all the things as it can do with simple variable of that type. Individual members of structure is accessed with. Operator

structure – variable. member – name



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext