2017-04-17 46 views
1

因此,我今天開始學習C,沒有任何問題通過補習練習,但我終於被卡住了。我檢查了stackoverflow,其他reddit帖子和youtube視頻。我正在嘗試創建一個鏈表。以下是我的代碼,詳細說明了我認爲代碼的作用。當我在CLion中運行它時,printList函數沒有輸出。但是,如果我取消註釋掉被削減的行(只有3行很容易找到),並且將我的調用註釋爲push(),printList函數會打印1,因爲它應該是這樣。據我瞭解,3條註釋行和push()中的行是做同樣的事情,那麼爲什麼輸出不同呢?更改指針值的問題

#include <stdio.h> 
#include <malloc.h> 

typedef struct node { 
    int val; 
    struct node *next; 
} node_t; //node struct is defined 

void printList(node_t *head); //printlist function is initialized, accepts a node_t pointer (correct vocabulary?) 
void push(node_t *head, int val); //push function is initialized 

int main() { 
    node_t *head = NULL; //a pointer is created that points to a struct of type node_t, and currently points to NULL 
    push(head, 1); //push function accepts node_t pointer which currently points to NULL, and an int: 1 
// head = (node_t *) malloc(sizeof(node_t)); //the pointer "head" now points to a section of memory that can 
    //hold a node_t struct 
// head->val = 1; //head's "val" variable now points to the int 1 
// head->next = NULL; // head's "next" variable now points to NULL 
    printList(head); 
    return 0; 
} 

void printList(node_t *head) { 
    node_t *current = head; 
    while (current != NULL) { 
     printf("%d ", current->val); 
     current = current->next; 
    } 
} 

void push(node_t *head, int val) { 
    node_t *current = head; //the pointer "current" now points to the value that head pointed to (NULL) 
    current = (node_t *) malloc(sizeof(node_t)); //just enough memory is allocated for a node_t struct, 
    // and the variable current now points to it 
    current->val = val; //current's "val" variable now points to the int "val" from the function parameters 
    current->next = NULL; //current's "next" variable, which is a node_t pointer, now points to NULL 

} 
+3

你不改變*頭*了'push'之外; 'push'中的'head'是一個局部變量。 –

+0

問題的一個部分:您直接在'current = malloc(...)'之後執行'current = head'。現在,第二次作業後的「current」點在哪裏? –

+0

問題的另一部分:搜索*模擬c *中的引用傳遞。 –

回答

3

從註釋繼續,如果你打算使用一個void功能,而不是返回node_t *並分配給head,你需要的head地址傳遞給push?爲什麼?當您爲push分配current時,它有自己的地址,並且與中的main無關。

當分配node_t *current = head;你到current分配頭副本(不頭本身)。現在,拷貝被初始化爲NULL,但是push中的指針head的地址是不是相同的headmain

你通過了什麼push? (ANS:的head副本,例如NULL指針)headpush具有獨立且不同的指針地址mainhead。當push的回報,你有pushheadcurrent),因爲current是內push聲明沒有提及任何指針和地址的head在推動,由編譯器創建的副本時,head被作爲參數傳遞,也走了。若要使在push中的地址與main中的head具有相同的地址,請將其地址傳遞給push,例如,

int main (void) { 

    node_t *head = NULL; 

    push (&head, 1); 
    printList (head); 

    putchar ('\n'); /* tidy up */ 

    return 0; 
} 
... 
void push(node_t **head, int val) 
{ 
    node_t *current = malloc(sizeof *current); 
    current->val = val; 
    current->next = NULL; 
    *head = current; 
} 

讓我知道你是否還有其他問題。


還可以返回一個指針(類型node_t *)和main分配,例如

int main (void) { 

    node_t *head = NULL; 

    head = push (1); 
    printList (head); 

    putchar ('\n'); /* tidy up */ 

    return 0; 
} 
... 
node_t *push(int val) 
{ 
    node_t *current = malloc(sizeof *current); 

    current->val = val; 
    current->next = NULL; 

    return current; 
} 
0

的關鍵問題是按值傳遞的指針不是地址。所以當調用者退出時,指針(頭部)的值根本不會改變!

你可以改變push這樣的:

void push(node_t **head, int val) { 
(*head) = (node_t *) malloc(sizeof(node_t)); //just enough memory is allocated for a node_t struct, 
(*head)->val = val; //current's "val" variable now points to the int "val" from the function parameters 
(*head)->next = NULL; //current's "next" variable, which is a node_t pointer, now points to NULL 
} 

那麼你可以這樣調用:push(&head, 1);

在這個例子中,我通過地址傳遞head並通過反引用push功能更新它的值。所以它的變化可以從push函數中看出。

希望它有幫助!

(我的計算器上的第一個答案:P)