title: Memory Allocation for Function Pointer
body: <p>I have a struct called warehouse and a generic linked list and each item points to a warehouse struct. </p>
  
  <pre><code>typedef struct linked{
      char type;
      void * item;
      struct linked * next;
      struct linked * prev;
  }LinkedList;
  
  
  typedef struct warehouse{
      char * name;
      float volume;
      float (* getPrice) (void * S);
      float (* getTotalDollarAmount)(void * S);
  }house_t;
  </code></pre>
  
  <p>When I tried to get the <code>getPrice</code> function pointer point to a function <code>float price (void *S)</code></p>
  
  <pre><code>void menu (LinkedList *house){
      char *c;
      float num;
      c = (char*)malloc(sizeof(char)*10);
  
      LinkedList *i;
      i = (LinkedList*)malloc(sizeof(LinkedList);
      house_t *sk;
      sk = (house_t *) malloc(sizeof(house_t));
      //i-&gt;item = (house_t *) malloc(sizeof(house_t));
  
      scanf("%c", c);
  
      ((house_t*)i-&gt;item)-&gt;getPrice = price;
      sk=findhouse(house, c);
      num = ((house_t*)i-&gt;item)-&gt;getPrice(sk);
      printf("%f",num);
  }
  </code></pre>
  
  <p>I got bad access error. Since every time I got a bad access error it was because I didn't allocate memory for something. But do I need to allocate memory for function pointer? If so, how?</p>
  
  <p>Here's some more code</p>
  
  <pre><code>float price (void *S)
  {
      return ((house_t*)S)-&gt;volume;
  }
  </code></pre>