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;
}
Discussion:
17 comments Page 1 of 2.
Srividhya said:
2 decades ago
Entire structure cannot be compared. It can be done only on a member wise comparison.
Zombie said:
1 decade ago
Why can't structures be compared? They are "value type variables" ?
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.
Arun said:
1 decade ago
It is not possible in case of structures?
Jains said:
1 decade ago
Ya it is not!
Vanitadevi said:
1 decade ago
I need more explanation for this can any one explain?
Atul said:
1 decade ago
Can anyone explain why we can't compare structure?
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
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.
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.
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".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers