作者HILL33LOVE (就是愛希爾)
看板C_and_CPP
標題[問題] 在C語言如何把linked list傳到function
時間Mon Dec 3 22:05:37 2012
想請教一下
我現在有個link list如下面程式碼
假設我現在有需要呼叫其它的api的時候
怎麼把link list傳到api裡面呢?
是要把head,current prev三個參數都帶進去嗎?
謝謝!!
struct gvsendlist
{
int no;
gvpacket_t gv;
struct gvsendlist *next;
};
struct gvsendlist *head = NULL;
struct gvsendlist *current = NULL;
struct gvsendlist *prev = NULL;
// input data
int idx = 0;
for(idx = 0; idx < 4; idx++)
{
current = (struct gvsendlist *)malloc(sizeof(struct gvsendlist));
if (current == NULL)
return(0);
current->next = NULL;
current->no = 4;
strncpy(current->gv.SetIP, "192.168.0.1", sizeof("192.168.0.1"));
if (head == NULL)
head = current;
else
prev->next = current;
prev = current;
}
// display linked list
current = head;
while(current != NULL)
{
printf("No. = %d, SetIP = %s\n", current->no, current->gv.SetIP);
current = current->next;
}
// free linked list
current = head;
while(current != NULL)
{
prev = current;
current = current->next;
free(prev);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.248.143.163
推 EmptySmile:請問api是您撰寫的還是C/C++ library? 12/03 23:06
→ EmptySmile:喔...我看到了library的...抱歉... 12/03 23:06
→ EmptySmile:請問錯誤是什麼? 12/03 23:08
推 EmptySmile:請問gv.SetIP是char*型態嗎? 12/03 23:12
→ EdisonX:話說你的 api 長怎樣?方便先給 prototype ? 12/04 02:06