C Quiz – 101 | Question 3

filter_none

edit
close

play_arrow

link
brightness_4
code

#include "stdlib.h"
int main()
{
 int *pInt;
 int **ppInt1;
 int **ppInt2;
  
 pInt = (int*)malloc(sizeof(int));
 ppInt1 = (int**)malloc(10*sizeof(int*));
 ppInt2 = (int**)malloc(10*sizeof(int*));
  
 free(pInt);
 free(ppInt1);
 free(*ppInt2);
 return 0;
}

chevron_right


Choose the correct statement w.r.t. above C program.
(A) malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
(B) free(*ppInt2) is not correct. It’ll give compile time error.
(C) free(*ppInt2) is not correct. It’ll give run time error.
(D) No issue with any of the malloc() and free() i.e. no compile/run time error.


Answer: (D)

Explanation: ppInt2 is pointer to pointer to int. *ppInt2 is pointer to int. So long as the argument of free() is pointer, there’s no issue. That’s why B) and C) both are not correct. Allocation of both ppInt1 and ppInt2 is fine as per their data type. So A) is also not correct. The correct statement is D).

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.