© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

10. How can we check whether the contents of two structure variables are same or not? 

Ans:
       There is no way to compare entire structure, we can only compare element by    element.
 
example:
typedef struct{
int a;
int b;
} ravi_t;
main()
{
ravi_t r;
ravi_t s;
/* this is not allowed */
if(r==s) /* this is not allowed */;
/* where as we can compare element by element */
if(r.a == s.a);  

Ex - 1:  Is the following code legal?
struct a
    {
int x;
struct a b;
   }
Ans:
            No
Explanation:

Is it not legal for a structure to contain a member that is of the same type as in this case. Because this will cause the structure declaration to be recursive without end. 

Ex - 2:  Is the following code legal?
struct a
    {
int x;
            struct a *b;
    }
Ans:
Yes.
Explanation:

*b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure is determined(as you know the pointer to any type is of same size). This type of structures is known as ‘self-referencing’ structure.

Ex - 3:    Is the following code legal?
typedef struct a
    {
   int x;
   aType *b;
    }aType
Ans:
            No
Explanation:

The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs).


SLogix Student Projects
bottom