1

在下面的代码中,由 '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);
4

3 回答 3

2

你永远不会初始化root->maskAddr.

于 2013-03-17T17:31:56.783 回答
2

实际上你没有分配给root->maskAddr = ;

后:

root = malloc(sizeof(struct RouteInfo));
root->next = 0;
root->netAddr = 19216811;

添加:

root->maskAddr = 25525555;   

及其工作:

:~$ ./a.out 
19216811 25525555 

在这里您可以找到您的工作代码:键盘

于 2013-03-17T17:42:09.113 回答
1

正如 NPE 所说,您从不重视root->maskAddr. 所以也许你的意思是这个?

printf("%lu%lu \t",root->netAddr, pointer->next->maskAddr);
于 2013-03-17T17:39:16.367 回答