Q:

Why is setting a derefernce pointer equal to a primitive illegal?

Why does setting the value of a dereferenced pointer raise a Segmentation fault 11? To make what I mean clear look a the follow code:
#include <stdio.h>

int *ptr;
*ptr = 2;

int main(){
  printf("%d\n", *ptr);
  return 0;
}

I thought that *ptr=2 would set the rvalue that the pointer ptr is point to to 2. Is that not the case? I apologize if for those c expert programmers, this is really easy/obvious.
Are we only allowed to set a dereferenced pointer (i.e. *ptr) to a value if that value had a memory address? i.e. like doing:
int k = 7;
int *ptr = k;

and then:
*ptr = 2;

A:

The problem here is that ptr is not pointing to allocated space. See the following:
#include <stdio.h>
#include <stdlib.h> 

int main(void){

  // Create a pointer to an integer.
  // This pointer will point to some random (likely unallocated) memory address.
  // Trying set the value at this memory address will almost certainly cause a segfault.
  int *ptr;

  // Create a new integer on the heap, and assign its address to ptr.
  // Don't forget to call free() on it later!
  ptr = malloc(sizeof(*ptr)); 

  // Alternatively, we could create a new integer on the stack, and have
  // ptr point to this.
  int value;
  ptr = &value;

  // Set the value of our new integer to 2.
  *ptr = 2;

  // Print out the value at our now properly set integer.
  printf("%d\n", *ptr);

  return 0;
} 

