c - ListEntry class usage -
#include <windows.h> #include "stdafx.h" #include "list.h" typedef struct list_node { list_entry list; int val1; }list_node, *plist_node; int _tmain(int argc, _tchar* argv[]) { int = 0; plist_node pnewnode; pnewnode = new list_node; list_entry head; initializelisthead(&head); (i = 0; < 3; i++) { pnewnode = new list_node; pnewnode->val1 = i; inserttaillist(&head, &pnewnode->list); pnewnode = null; } while (!islistempty(&head)) { plist_entry removenode = removeheadlist(&head); plist_node mydatanode = (plist_node)containing_record (removenode,list_node,val1); printf("%d\n", mydatanode->val1); } return 0; }
with code not getting val1 data gives me junk value?any thing wrong doing?
please read documentation containing_record. third parameter described as:
field [in]
the name of field pointed address , contained in structure of type type.
i suspect code:
plist_entry removenode = removeheadlist(&head); plist_node mydatanode = (plist_node)containing_record(removenode,list_node,val1);
is incorrect. because third parameter containing_record
not of same type address passing in. doesn't make sense val1
either. needs figure out address of list_node
address of list_entry
contained in list_node
.
i think using:
plist_entry removenode = removeheadlist(&head); plist_node mydatanode = (plist_node)containing_record(removenode,list_node,list);
should solve problem.
Comments
Post a Comment