<div dir="ltr"><div>I have a linked list like this <a href="http://pastebin.com/bwF3jLb6">http://pastebin.com/bwF3jLb6</a> . The problem i am facing is i have initialized the list head and</div><div> then i am doing malloc for that struct object. What i need is i want to make struct abc_st *r as head of linked list and other nodes after it.</div><div><br></div><div>typedef struct abc {</div><div><span class="" style="white-space:pre">        </span>char client_id[18];</div><div>  Â  char mac[18];</div><div>  Â  st_list_head list;</div><div>}abc_st;</div><div><br></div><div>abc_st* check_fields (abc_st *ptr)</div><div>{</div><div>  Â  char cmd[500];</div><div>  Â  MYSQL_RES *result;</div><div>  Â  MYSQL_ROW row;</div><div>  Â  int num_rows;</div><div>  Â  abc_st *r=NULL,*temp=NULL;</div><div>  Â  INIT_LIST_HEAD(&amp;r-&gt;list); Â  // i have intialized the head here</div><div><br></div><div>  Â  sprintf(cmd, &quot;SELECT * FROM ABCD_TABLE WHERE MAC = %.4x&quot;, ptr-&gt;mac);</div><div>  Â  Â  Â  /* Running the sql query to check for fields with value in database */</div><div>  Â  if (mysql_query(abc_db.db_handle, cmd)) {</div><div>  Â  Â  Â  num_rows = -1;</div><div>  Â  Â  Â  goto done;</div><div>  Â  }</div><div>  Â  result = mysql_store_result(abc_db.db_handle);</div><div>  Â  if (result == NULL) {</div><div>  Â  Â  Â  num_rows = -1;</div><div>  Â  Â  Â  goto done;</div><div>  Â  }</div><div>  Â  num_rows = mysql_num_rows(result);</div><div><br></div><div>  Â  while ((row = mysql_fetch_row(result)))</div><div>  Â  {</div><div>  Â  Â  Â  r= (abc_st *)malloc(sizeof(abc_st));</div><div>  Â  Â  Â  memcpy(r-&gt;mac,row[1],strlen(row[1])+1);</div><div>  Â  Â  Â  memcpy(r-&gt;client_id,row[0],strlen(row[0])+1);</div><div>How should i use list_add ?</div><div>  Â  Â  Â  //list_add(struct list_head *new, struct list_head *head) Â //prototype of list_add</div><div>  Â  Â  Â // list_add(&amp;temp-&gt;list,&amp;r-&gt;list); Â //it will go wrong here, i want to make r(structure r as head and then add other strutcure objects) Â </div><div>  Â  Â // Â  r-&gt;link = NULL;</div><div>  Â  Â  Â  //list_add(r, ptr);</div><div><br></div><div>  Â  }</div><div>done:</div><div>  Â  mysql_free_result(result);</div><div><br></div><div>  Â  return r;</div><div><br></div><div>}</div><div><br></div></div>