title: Directly assigning values to C Pointers
body: <p>I've just started learning C and I've been running some simple programs using MinGW for Windows to understand how pointers work. I tried the following:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(){
      int *ptr;
      *ptr = 20;
      printf("%d", *ptr);
      return 0;
  }
  </code></pre>
  
  <p>which compiled properly but when I run the executable it doesn't work - the value isn't printed to the command line, instead I get an error message that says the .exe file has stopped working.</p>
  
  <p>However when I tried storing the value in an int variable and assign *ptr to the memory address of that variable as shown below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(){
      int *ptr;
      int q = 50;
      ptr = &amp;q;
      printf("%d", *ptr);
      return 0;
  }
  </code></pre>
  
  <p>it works fine.</p>
  
  <p>My question is, why am I unable to directly set a literal value to the pointer? I've looked at tutorials online for pointers and most of them do it the same way as the second example.</p>
  
  <p>Any help is appreciated. </p>