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

C and C++ Interview Questions

2. Explain with an example the self-referential structure.
Ans:
A self referential structure is used to create data structures like linked lists, stacks, etc. Following is an example of this kind of structure:
struct struct_name
{
       datatype datatypename;
       struct_name * pointer_name;
};

Ex - 1:  What is the output?
struct point
 {
 int x;
 int y;
 };
struct point origin,*pp;
main()
{
pp=&origin;
printf("origin is(%d%d)\n",(*pp).x,(*pp).y);
printf("origin is (%d%d)\n",pp->x,pp->y);
}
           
Ans:
origin is(0,0)
origin is(0,0)

Explanation:

pp is a pointer to structure. we can access the elements of the structure either with arrow mark or with indirection operator.
Note:
Since structure point  is globally declared x & y are initialized as zeroes

Ex - 2:  What is the output for the program given below
 
     typedef enum errorType{warning, error, exception,}error;
     main()
    {
        error g1;
        g1=1;
        printf("%d",g1);
     }

Ans:
Compiler error: Multiple declaration for error

Explanation

The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the compiler cannot distinguish the meaning of error to know in what sense the error is used:
            error g1;
g1=error;
            // which error it refers in each case?
When the compiler can distinguish between usages then it will not issue error (in pure technical terms, names can only be overloaded in different namespaces). Note: the extra comma in the declaration, enum errorType{warning, error, exception,} is not an error. An extra comma is valid and is provided just for programmer’s convenience.


SLogix Student Projects
bottom