title: Difference between definition of a structure in C
body: <p>I am starting to grasp how Linked List work in C and I have a doubt about how to build a function that generate new elements for a linked list:</p>
  <pre><code>struct elem{
    int data;
    struct elem* next;
  };
  
  
  typedef struct elem* listEl; // we call data type pointer to an element listEl;
  
  
  //how to create an element
  
  listEl createElem1(){
    listEl temp;
    temp = malloc(sizeof(struct elem)); 
  
    (*temp).data = 0;
    (*temp).next = NULL;
    return temp;
  
  }
  
  //how to create an element
  
  listEl createElem2(){
    listEl temp;
    temp = malloc(sizeof(struct elem*)); ////
  
    (*temp).data = 0;
    (*temp).next = NULL;
    return temp;
  
  }
  </code></pre>
  <p>Here we have defined an element of a linked list as a structure with an integer and a pointer to another element of a linked list.</p>
  <p>The other two functions are needed to generate new pointer to elements. Since we want to generate a new element, should we use to allocate the relevant memory for our element the function <code>&quot;malloc(sizeof(struct elem))&quot;</code> while on the other hand the function <code>malloc(sizeof(struct elem*));</code> would be an error, since we are allocating the size of a pointer and not the size of a struct?</p>
  <p>I am asking this since in one of the lectures I am following about this topic a function to create new element has been defined in the second way and it seemed to me not correct.</p>
  <p>Thanks in advance</p>