C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 9)
9.
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char n[20];
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    if(e1 == e2)
        printf("The structure are equal");
    return 0;
}
Prints: The structure are equal
Error: Structure cannot be compared using '=='
No output
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

Alfaz said:   1 decade ago
Why we are not using '==' to compare the structure is if any of the structure contains union, floating point variables, pointers etc. Structure that does not contain this variables then we can directly go for field by field comparison or using

memset (&myStruct, 0, sizeof(myStruct));

This will set the whole structure (including the padding) to all-bits-zero. We can then do a memcmp() on two such structures.

memcmp (&s1,&s2,sizeof(s1));

Shalivahana nandish said:   9 years ago
You should not compare structure variables directly,using *pointer variables comparing two varables is possible .ex is given below.

#include <stdio.h>
int main()
{
struct emp
{
char n[20];
int age;
};
struct emp *e1 = {"Dravid", 23};
struct emp *e2 = e1;
if(e1<=e2)
printf("The structure are equal");
return 0;
}
op:The structures are equal.

Robert said:   9 years ago
Comparing the structures themselves as variables won`t work.

You have to do:
#include

int main()
{
struct emp
{
char n[20];
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
if(e1.n == e2.n, e1.age == e2.age)
printf("The structure are equal");
return 0;
}

Compare each element of the structure.

Mumtaz said:   1 decade ago
No, you cannot!
The only way to compare two structures is to write your own function that compares the structures field by field. Also, the comparison should be only on fields that contain data (You would not want to compare the next fields of each structure!).

Yakesh said:   1 decade ago
We need to compare like this if we want "structurevar.strmember==strvar.strmem"
struct emp e2 = e1;

In the above stmt we need not to write "struct emp" one structure variable content can be copied to another str var directly.

Neelkant said:   1 decade ago
Answer is just simple because the operator "==" is defined only to operate with the int, float and char, But not with strings, structure, unions or enums in C. In C++ it can be solved using operator overloading concept.

Sudharsan said:   1 decade ago
I can't agree what vinay said because in stucture it is possible to compare two variable in single structure and also we knew '==' the operator use for equal values between two variables.

Yakesh said:   1 decade ago
Structure variables can be compared usig == oprator but most of the compilers will not support this operation, so the compiler will generate an error like"Illegal structure operation".

Vinay said:   1 decade ago
Structure cannot be compared here because you are assigning the same value of e1 to the e2 which is not allowed, since it is possible in some cases but only if you have * variables.

Wozniak said:   1 decade ago
We should not compare structures because of the holes between the member fields of a structure.


Post your comments here:

Your comments will be displayed after verification.