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

C and C++ Interview Questions

14. What is indirection?

Ans:

If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value.

Ex - 1:  What is the output?
void main()
{
printf(“sizeof (void *) = %d \n“, sizeof( void *));
            printf(“sizeof (int *)    = %d \n”, sizeof(int *));
            printf(“sizeof (double *)  = %d \n”, sizeof(double *));
            printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *));
            }

Ans :
sizeof (void *) = 2
sizeof (int *)    = 2
sizeof (double *)  =  2
sizeof(struct unknown *) =  2

Explanation:
The pointer to any type is of same size.

Ex - 2 what is the output?:

void main()
{
int i=10, j=2;
int *ip= &i, *jp = &j;
int k = *ip/*jp;
printf(“%d”,k);
}      
  
Ans:
Compiler Error: “Unexpected end of file in comment started in line 5”.

Explanation:

The programmer intended to divide two integers, but by the “maximum munch” rule, the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. To force what is intended by the programmer,
int k = *ip/ *jp;           
// give space explicity separating / and *
//or
int k = *ip/(*jp);
// put braces to force the intention 
will solve the problem. 


SLogix Student Projects
bottom