2013-03-17 99 views
1

在由'root'指向的'netAddr'下面的代碼中打印出來,而不是'指針'指向的'maskAddr'。在此先感謝,只是要求作爲最後的手段。 'netAddr'的值會打印出來,但'指針'指向的任何東西都不會。爲什麼此結構的這個成員不打印出來(C編程)

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); 

回答

2

其實你不分配給root->maskAddr = ;

後:

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

地址:

root->maskAddr = 25525555; 

及其工作:

:~$ ./a.out 
19216811 25525555 

在這裏你可以找到你的工作代碼:Codepad

+0

謝謝,你有什麼想法我可以得到'ifname'打印出來嗎? 到目前爲止,我已經將它指定爲「strcpy(root-> ifName,」dtfdty「);」和「root-> ifName,」dtfdty「;」但是他們沒有一個是編譯器說他們不兼容的。 – 2013-03-17 17:52:28

+0

@ACScStudent ----是的%s,看到這個[鍵盤](http://codepad.org/c1yJkwkl) – 2013-03-17 17:54:54

+1

謝謝,我得到它的工作之前,我應該先嚐試更多,謝謝你的所有幫助。 – 2013-03-17 17:58:00

2

您從不初始化root->maskAddr

1

由於NPE表示,您從未估價過root->maskAddr。所以也許你是這個意思?

printf("%lu%lu \t",root->netAddr, pointer->next->maskAddr);