下面的代码中,'root'指向的'netAddr'打印的是put,而不是'pointer '指向的'maskAddr'。提前感谢,万不得已才问。'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);
实际上您没有分配给root->maskAddr = ;
root = malloc(sizeof(struct RouteInfo));
root->next = 0;
root->netAddr = 19216811;
添加:
root->maskAddr = 25525555;
和它的工作:
:~$ ./a.out
19216811 25525555
在这里可以找到你的工作代码:Codepad
不初始化root->maskAddr
正如NPE所说,你从未重视过root->maskAddr
。也许你是这个意思?
printf("%lu%lu t",root->netAddr, pointer->next->maskAddr);