Q:

How pointers allocate memory

#include<stdio.h>
int main()
{

  char *str;

  gets(str);

  puts(str);

return 0;
}

output= Segmentation Fault
Why it is giving segmentation fault?

A:

The answer to the question "how pointers allocate memory" is very simple: they don't. That's why you are getting a segmentation fault: accessing data from an uninitialized pointer is undefined behavior. 
It is programmer who has to allocate memory, and assign it to the pointer. For example, you could use malloc:
char *str = malloc(10);
fgets(str, 10, stdin);
puts(str);

An important thing to note about the above fix is that fgets is used instead of deprecated gets. This is because fgets lets you tell the function how much memory it can use in your buffer, while gets assumes that it has all the memory it wants, leading to buffer overruns.

