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

C and C++ Interview Questions

Ex 8: What is the output?

       main()
{
 int  i, n;
 char *x = “girl”;
 n = strlen(x);
 *x = x[n];
 for(i=0; i
   {
printf(“%s\n”,x);
x++;
   }
 }

Ans:
(blank space)
irl
rl
l
 
Explanation:

Here a string (a pointer to char) is initialized with a value “girl”.  The strlen function returns the length of the string, thus n has a value 4. The next statement assigns value at the nth location (‘\0’) to the first location. Now the string becomes “\0irl” . Now the printf statement prints the string after each iteration it increments it starting position.  Loop starts from 0 to 4. The first time x[0] = ‘\0’ hence it prints nothing and pointer value is incremented. The second time it prints from x[1] i.e “irl” and the third time it prints “rl” and the last time it prints “l” and the loop terminates.

Ex - 9:  What is the output?
main ( )
{
 static char *s[ ]  = {“black”, “white”, “yellow”, “violet”};
 char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
 p = ptr;
 **++p;
 printf(“%s”,*--*++p + 3);
}

Ans:
            ck

Explanation:

In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p =  s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 – 1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is ‘ck’.


1 >> 2 >> 3 >> 4 >> 5 >> 6 >> 7 >> 8 >> 9
SLogix Student Projects
bottom