3i Infotech Placement: Sample Questions 190 - 191 of 1245

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 190

Edit

Describe in Detail

Essay▾

Predict the output or error (s) for the following:

  1. char ⚹someFun1()
  2. { chartemp[ ] =“string";returntemp; } char ⚹someFun2() { chartemp[ ] ={‘s’, ‘t’,’r’,’i’,’n’,’g’}; returntemp; }intmain()
  3. { puts(someFun1()); puts(someFun2()); }

Explanation

  • In the program both the functions suffer from the problem of dangling pointers
Table Showing the Program
char ⚹ someFun1 ()

{char temp [] = “string” ; return temp;}

  • In someFun1 () temp is a character array
  • The space for it is allocated in heap and is initialized with character string “string” .
  • This is created dynamically as the function is called, so is also deleted dynamically on exiting the function
  • So the string data is not available in the calling function main () leading to printing of garbage values.
char ⚹ someFun2 () {char temp [] = { ‘s’ , ‘t’ , ‘r’ , ‘i’ , ‘n’ , ‘g’ } ; return temp;}
  • The function someFun2 () also suffers from the same problem but the problem can be easily identified in this case.

Question 191

Edit

Describe in Detail

Essay▾

Predict the output or error (s) for the following:

  1. main ()
  2. {
  3.     inti =258;
  4.     int ⚹iPtr =&i;
  5.     printf ( “%d %d” ,⚹( (char ⚹) iPtr), ⚹( (char ⚹) iPtr +1) );
  6. }

Explanation

In the program

Table Shows the Program
int i = 258;
  • Given the integer variable i = 258
int ⚹ iPtr = &i;
  • The integer value 257 can be represented in binary as, 00000001 00000001.
  • INTEL machines are ‘small-endian’ machines- lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses.
  • The integer value 258 is stored in memory as: 00000001 00000010.
printf ( “% d % d” , ⚹ ( (char ⚹) iPtr) , ⚹ ( (char ⚹) iPtr + 1) ) ;
  • So the individual bytes taken by casting it to char ⚹ get printed as 2 1

Developed by: