```
//C - code 
int num
int *pi = &num; //1 
printf(" *pi=%d  and  pi=%d \n ", *pi, pi);  // p1
pi = &num;      //2
printf(" *pi=%d  and  pi=%d \n ", *pi, pi);  // p2

```

As for comment `2`, I understand that we are assigning a valid address to the pointer pi but I don't understand what is happening in the line of comment `1` because because both the `printf()` statements `p1` and `p2` are doing the same job. 

And in case of **dynamic memory management** with *malloc(sizeof(datatype))* function, why the following is an invalid approach?

```
int *pi;
*pi = (int*)malloc(sizeof(int)); // terminates the program 

```

while the following:

```
int *ri = (int*)malloc(sizeof(int)); 

```

and

```
int *qi;
qi = (int*)malloc(sizeof(int));

```

works fine. 

Please explain. 

**EDIT :** The question marked as duplicate to this question doesn't talk about pointers and problems that we face during dynamic memory management.