在下面的代码中,由 'root' 指向的 'netAddr' 打印 put 但不是由 'pointer. 在此先感谢,仅作为最后的手段。'netAddr' 的值会打印出来,但 'pointer' 指向的任何东西都不会。
struct RouteInfo
{
unsigned long netAddr;
unsigned long gateAddr;
unsigned long maskAddr;
char ifName[IF_NAMESIZE];
int metric;
int ttl;
struct RouteInfo *next;
};
struct RouteInfo *root; /* This will be the unchanging first node */
/* This will point to each node as it traverses the list */
struct RouteInfo *pointer;
//allocating space for the root
root = malloc(sizeof(struct RouteInfo));
root->next = 0;
root->netAddr = 19216811;
//Point to 1st Item
pointer = root;
/* Creates a node at the end of the list */
pointer->next = malloc( sizeof(struct RouteInfo) );
pointer = pointer->next;
pointer->maskAddr = 25525500;
pointer->next = malloc( sizeof(struct RouteInfo) );
pointer = pointer->next;
pointer->gateAddr = 19216810;
pointer->next = malloc( sizeof(struct RouteInfo) );
pointer = pointer->next;
strcpy(pointer->ifName, "eth1");
pointer->next = malloc( sizeof(struct RouteInfo) );
pointer = pointer->next;
pointer->metric = 16;
pointer->next = malloc( sizeof(struct RouteInfo) );
pointer = pointer->next;
pointer->ttl = 30;
//marking the last as NULL
pointer->next = 0;
//Pointer points to root again
pointer = root;
printf("%lu%lu \t",root->netAddr, pointer->maskAddr);