What is the output of the program?#include<stdio.h>#include<conio.h>void main(){char *st1[3]= {"Hello","World","Oracle"};*st1=st1[2];st1[1]=*st1;free(st1[0]);free(st1[1]);clrscr();printf("%s %s %s",st1,st1[1],st1[2]);getch();}A. Garbage Garbage OracleB. Oracle Oracle OracleC. Hello World OracleD. Core Dump cannot print after freeing the memory

D

This question is related to Oracle Interview

Showing Answers 1 - 1 of 1 Answers

vikas

  • Sep 11th, 2006
 

it ll display a blank screen. u can figure it out if u look at the code :

char *st1[3]= {"Hello","World","Oracle"};// here st1[0] points to Hello, st1[1] points to World and so on !!

*st1=st1[2];// here we lost st1 (i.e. "hello" which was at base add) ..st1 points to "oracle"
st1[1]=*st1;// nw even st1[1] points to "oracle"
free(st1[0]);// here we free st1[0], i.e. string pointed by base add
free(st1[1]);// here we free st1[1] also
clrscr();

in the end there is nothin pointed by st1, or st1[1] or st1[2]... we freed all the strings pointed by them.

The concept is pretty much simple here. st1 is just an array of pointers which will hold address of 3 different strings. but before printing, we are freeing them all !!!

 

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions