2016-01-13 77 views
1

我試圖讓輸入的讀數從動態分配標準輸入兩種不同方式的全局變量的realloc:malloc和在功能上

1)我從標準輸入,的malloc,realloc的讀取和寫入到陣列主要。然後當我printf這些數組的元素時,一切都很好。

2)我在main中聲明瞭全局變量,然後把它扔到函數中,我從stdin,malloc,realloc讀入並寫入該數組。當我在函數中打印這些元素時,它可以正常工作,但是當我在函數外部執行操作時,它會給我分段錯誤。

你可以檢查這些代碼,並幫助我如何解決這個問題?

2)

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

/*--------------------------------------------------------------------------*/ 

void nacitaj(int *pocet_slov, char **slovnik){ 

    char buffer[60]; 
    int size = 60; 

    while(fgets(buffer, size, stdin) != NULL){ 
     *pocet_slov = *pocet_slov + 1; 
     slovnik = (char**) realloc(slovnik, (*pocet_slov+1) * sizeof(char*)); 
     slovnik[*pocet_slov-1] = (char*) malloc(strlen(buffer) * sizeof(char)); 
     buffer[strlen(buffer)-1] = '\0'; 
     strcpy(slovnik[*pocet_slov-1], buffer); 
    } 

    for(int i = 0; i < *pocet_slov; i ++){ 
     printf("%d. %s\n", i+1, slovnik[i]); 
    } 
} 

/*---------------------------------------------------------------------------*/ 

int main(){ 
    char **slovnik = NULL; 

    int pocet_slov = 0; 

    int i; 

    printf("Zadavaj slova do slovniku:\n"); 
    nacitaj(&pocet_slov, slovnik); 

    for(i = 0; i < pocet_slov; i ++){ 
     printf("%d. %s\n", i+1, slovnik[i]); 
    } 

    return 0; 
} 

1)輸入的

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main(){ 
    char **slovnik = NULL; 

    int pocet_slov = 0; 

    char buffer[60]; 
    int size = 60;  
    int i; 

    printf("Zadavaj slova do slovniku:\n"); 

    while(fgets(buffer, size, stdin) != NULL){ 
     pocet_slov ++; 
     slovnik = (char**) realloc(slovnik, (pocet_slov+1) * sizeof(char*)); 
     slovnik[pocet_slov-1] = (char*) malloc(strlen(buffer) * sizeof(char)); 
     buffer[strlen(buffer)-1] = '\0'; 
     strcpy(slovnik[pocet_slov-1], buffer); 
    } 

    for(i = 0; i < pocet_slov; i ++){ 
     printf("%d. %s\n", i+1, slovnik[i]); 
    } 

    return 0; 
} 

實施例:

car 
bird 
pen 
+0

您的輸入是什麼? – dbush

+0

我的輸入是「導入」到詞彙表中的單詞..例如: car \ nbird \ npen – alik33

+1

我的意思是具體。程序的確切輸入是什麼2)導致它發生段錯誤?並請更新您的問題與此信息。 – dbush

回答

2

您應該將slovnik的地址傳遞給nacitaj函數。否則,更新nacitaj中的變量在main中不可見。

此外,永遠記得free任何動態分配的內存。

void nacitaj(int *pocet_slov, char ***slovnik){ 

    char buffer[60]; 
    int size = 60; 

    while(fgets(buffer, size, stdin) != NULL){ 
     *pocet_slov = *pocet_slov + 1; 
     // dereference slovnik to update the variable in main 
     // don't cast the return value of malloc or realloc 
     *slovnik = realloc(*slovnik, (*pocet_slov+1) * sizeof(char*)); 
     (*slovnik)[*pocet_slov-1] = (char*) malloc(strlen(buffer) * sizeof(char)); 
     buffer[strlen(buffer)-1] = '\0'; 
     strcpy((*slovnik)[*pocet_slov-1], buffer); 
    } 

    for(int i = 0; i < *pocet_slov; i ++){ 
     printf("%d. %s\n", i+1, (*slovnik)[i]); 
    } 
} 

int main(){ 
    char **slovnik = NULL; 

    int pocet_slov = 0; 

    int i; 

    printf("Zadavaj slova do slovniku:\n"); 
    // pass the address of slovnik 
    nacitaj(&pocet_slov, &slovnik); 

    for(i = 0; i < pocet_slov; i ++){ 
     printf("%d. %s\n", i+1, slovnik[i]); 
    } 

    // free the allocated memory 
    for(i = 0; i < pocet_slov; i ++){ 
     free(slovnik[i]); 
    } 
    free(slovnik); 

    return 0; 
} 
+0

非常感謝你,這是有益的(y)..順便說一句,我知道釋放記憶。我只是想繼續這樣做,所以我還沒有做到 – alik33

-1

在代碼編號2)你沒有定義一個局部變量。之前主要適用範圍,例如應聲明的全局變量:

#include <stdio.h> 

static int shared = 3; 

void add(void){ 
    shared++; 
} 

int main(void){ 
    printf("%d", shared)// will print 3 
    add(); 
    printf("%d", shared)// will print 4 

    return 0; 
} 

這僅僅是達到你想要的東西,因爲只有教育目的的例子。你應該對全局變量非常仔細。

+0

請縮進您的代碼。 –

+0

你現在快樂嗎? –

+0

是的,好多了,但你的回答實際上不是OP問題的答案。 –