Regarding pointers MCQ ques 4 and 8

Please explain solution of 4 and 8.

Hi @kushagrathebest, the solution for the 2 questions are as follows:

Question 4:

Herein, the problem arises with line

int *ptr = (int *)malloc(sizeof(int));

Here what is happening is we created a pointer ptr of type int.
And then we say that ptr sould point to an int type data by means of:

(int *)malloc(sizeof(int))
We have already said/decleared that ptr will be an int type pointer but then also *(int )… is written. Here, memory is getting wasted. And thus it is memory leak. 4 bytes of memory is wasted here as we allocated a new place to ptr without properly deallocating it’s previous location.

Question 8:

We need to assign j and k their values seperately.
Just do this:

k = &a;
j = &a ;

instead of

j = k = &a

Hope you understand :slight_smile: