title: allocating memory to a struct using malloc
body: <p>I have a struct called State:</p>
  
  <pre><code>typedef struct State{
      char alphabets[2][6]; 
      struct State *PREV; /*this points to the previous state it came from*/
      struct State *NEXT; /*this points to the next state in the linked list*/
      int cost; /*Number of moves done to get to this position*/
      int zero_index;/*this holds the index to the empty postion*/
  } State;
  </code></pre>
  
  <p>Here's my memAllocator() method:</p>
  
  <pre><code>memAllocator(){
  struct State *p = (State*) malloc(sizeof(State));
  if (p==NULL){
          printf("Malloc for a new position failed");
          exit(1);
  }
  return p;
  </code></pre>
  
  <p>}
  Here's my main method.</p>
  
  <pre><code>main(){
  State *start_state_pointer=memAllocator();
  State start_state;
  start_state.zero_index=15;
  start_state.PREV = NULL;
  start_state.alphabets[0][0]='C';
  start_state.alphabets[0][1]='A';
  start_state.alphabets[0][2]='N';
  start_state.alphabets[0][3]='A';
  start_state.alphabets[0][4]='M';
  start_state.alphabets[0][5]='A';
  start_state.alphabets[1][0]='P';
  start_state.alphabets[1][1]='A';
  start_state.alphabets[1][2]='N';
  start_state.alphabets[1][3]='A';
  start_state.alphabets[1][4]='L';
  start_state.alphabets[1][5]='_';
  start_state_pointer=&amp;(start_state);
  /*start_state=*start_state_pointer;*/
  </code></pre>
  
  <p>}</p>
  
  <p>I think the statement start_state_pointer=&amp;(start_state); is just assigning the pointer start_state_pointer to to the small amount of temporary space created during State start_state, rather than to the space I allocated.
  But when I try the commented out statement start_state=*start_state_pointer to deference the pointer and allocate the space to start state. It gives me a segmentation fault.</p>
  
  <p>I am just starting out in C. Can some one help me with this? </p>