2011-04-02 46 views
0

我遇到了填充動態創建的字符串數組的問題。該數組基本上包含兩個字符串,我想用它們的長度來打印它們。我得到了一個非常奇怪的結果,即內存位置越來越混亂。請參閱下面的代碼。填充動態c數組時遇到問題

任何建議將不勝感激。謝謝。

void printWords(char **words, int numberOfWords) 
{ 
    int index; 

    for (index = 0; index < numberOfWords; index++) 
    { 
     printf("%s, %d\n", &(*words)[index], (int)strlen(&(*words)[index]));   
    } 
} 


void fillWords(char **words) 
{ 
    *words = malloc(2 * sizeof(char *)); 

    char hello[] = {"Hello"}; 
    (*words)[0] = (char)malloc(strlen(hello) * sizeof(char)); 
    strcpy(&(*words)[0], hello); //Copy word into array 

    char world[] = {"Worldz"}; 
    (*words)[1] = (char)malloc(strlen(world) * sizeof(char)); 
    strcpy(&(*words)[1], world); //Copy word into array 
} 


int main (int argc, const char * argv[]) 
{ 
    char *words; 

    fillWords(&words); 
    printWords(&words, 2); 

    return 0; 
} 

預期的輸出應該

Hello, 5 
Worldz, 6 

但是我得到

HWorldz, 7 
Worldz, 6 
+0

作業?沒問題,如果它是:) – pmg 2011-04-02 20:37:25

+0

哈哈,是的。我修好了它。 – David 2011-04-02 20:52:47

回答

1

我覺得你越來越char *char **之間的混淆。

此外,請確保爲字符串末尾的空終止字符分配足夠的內存。

這裏是我的解決方案:

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

void printWords(char **words, int numberOfWords) 
{ 
    int index; 

    for (index = 0; index < numberOfWords; index++) 
    { 
     printf("%s, %d\n", words[index], (int)strlen(words[index]));   
    } 
} 

char ** createWords() 
{ 
    char ** words; 

    // Allocate memory for an array of pointers, length 2. 
    words = malloc(2 * sizeof(char *)); 

    char hello[] = {"Hello"}; 
    words[0] = malloc((strlen(hello)+1) * sizeof(char)); 
    strcpy(words[0], hello); //Copy word into array 

    char world[] = {"Worldz"}; 
    words[1] = malloc((strlen(world)+1) * sizeof(char)); 
    strcpy(words[1], world); //Copy word into array 

    return words; 
} 


int main (int argc, const char * argv[]) 
{ 
    char **words; 

    words = createWords(); 
    printWords(words, 2); 

    return 0; 
} 

我改名fillWords到createWords,並使其返回一個指針,而不是採取一個指針作爲參數。如果你真的想fillword把一個指針作爲參數,你可以這樣做,但參數必須是char ***

+0

感謝您的更正。我撿起了我的錯誤。 – David 2011-04-03 16:34:52

1

第一個問題:你不能把2個字,char *(嗯......你可以,但不是你正在做的方式;而做到這一點的方式與你的代碼無關)

你需要一個「字符串數組」,或者,更多C-狀,char *數組:

char *words[2]; /* words[0] and words[1] are pointers to char */ 
char *wrong; /* wrong[0] is a char; wrong[1] is a char */ 

所以,在你的主要改變words的定義和檢查/編輯所有其它功能爲了正確。

int main (int argc, const char * argv[]) 
{ 
    char *words[2]; 

    fillWords(words); 
    printWords(words, 2); 

    return 0; 
} 
+0

其實我不想要一個指向char的指針。我想要一個字符數組。我想我解決了它。謝謝。 – David 2011-04-02 20:51:53

+0

你打算用「Hello \ x00Worldz」結束單個字符串:我的意思是嵌入式空終止符? – pmg 2011-04-02 20:55:12

+0

不,我的意圖是創建一個動態字符數組。數組的每個索引都包含一個字符串。請參閱上面的解決方案。 – David 2011-04-02 20:57:11

0

這是解決方案。我在char *和char之間混淆了。我想要的是一系列字符。

void printWords(char **words, int numberOfWords) 
{ 
    int index; 

    for (index = 0; index < numberOfWords; index++) 
    { 
     printf("%s, %d\n", words[index], (int)strlen(words[index]));   
    } 
} 


void fillWords(char **words) 
{ 
    *words = malloc(2 * sizeof(char *)); 

    char hello[] = {"Hello"}; 
    words[0] = malloc(strlen(hello) * sizeof(char)); 
    strcpy(words[0], hello); //Copy word into array 

    char world[] = {"Worldz"}; 
    words[1] = malloc(strlen(world) * sizeof(char)); 
    strcpy(words[1], world); //Copy word into array 
} 


int main (int argc, const char * argv[]) 
{ 
    char *words; 

    fillWords(&words); 
    printWords(&words, 2); 

    return 0; 
} 
+0

我只是跑了它,並沒有錯誤得到正確的輸出。你沒有提供答案。 – David 2011-04-02 21:05:34

+0

嗯......對不起,我誤解了。只有一個重要的錯誤:要保留字符串「Hello」,你需要6個字節(記住空終止符),但你只用'malloc(strlen(hello)* sizeof(char))'分配5。 – pmg 2011-04-02 21:11:56

+0

對不起大衛......這段代碼不會編譯(在包含stdio.h,string.h和malloc.h之後),它確實給出了正確的輸出,但我認爲這可能只是運氣。分配給'* word'應該和分配給word [0]一樣,這意味着你的第一個malloc語句沒有任何用處。我刪除了你的第一個malloc語句,結果是一樣的!這意味着你做錯了。我正在尋找答案。 – 2011-04-02 22:17:11