2013-05-27 44 views
0
typedef struct ArrayList 
{ 
    // We will store an array of strings (i.e., an array of char arrays) 
    char **array; 

    // Size of list (i.e., number of elements that have been added to the array) 
    int size; 

    // Length of the array (i.e., the array's current maximum capacity) 
    int capacity; 

} ArrayList; 

下面的函數應該動態地分配存儲器中以供其含有一個結構的報頭文件中支持字符串數組(見上文):當我試圖實現一個字符串數組時,我做了什麼錯誤?

void panic(char *s) 
{ 
    fprintf(stderr, "%s", s); 
    exit(1); 
} 

ArrayList *createArrayList(int length){ 
    ArrayList *n = malloc(sizeof(ArrayList)); 

    int initial = 0, i; 

    if (length > DEFAULT_INIT_LEN) 
    { 
     n->array = (char **)malloc(length * sizeof(int*)); 
     n->capacity = length; 

     for (i = 0; i< n->capacity; i++) 
     { 
      n->array[i] = NULL; 
     } 
    } 
    else 
    { 
     n->array = (char **)malloc(DEFAULT_INIT_LEN * sizeof(int*)); 
     n->capacity = DEFAULT_INIT_LEN; 

     for (i = 0; i< n->capacity; i++) 
     { 
      n->array[i] = NULL; 
     } 
    } 

    if (n->array == NULL) 
     panic("ERROR: out of memory in Mylist!\n"); 

    n->size = initial; 

    printf("-> Created new ArrayList of size %d\n", n->capacity); 
    return n; 
} 

然後我已經另一個功能是應該打印所有目前由createArrayList函數創建的新分配的數組中的字符串:

void printArrayList(ArrayList *list) 
{ 
    int i; 

    for(i=0; i<list->capacity; i++) 
    { 
     if (list->array[i] == NULL) 
      printf("(empty list)\n"); 
     else 
      printf("%s\n",list->array[i]); 

    } 
} 

當我實現printArrayList功能(上圖)在我的主要功能,輸出爲:

-> Created ArrayList of size 10 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 

但是,如果我在createArrayList功能測試二維數組的持有串輸出能力的一種手段插入strcpy(n->array[1], "apple");是:

-> Created ArrayList of size 10 

...然後崩潰

所以我的問題是我做錯了什麼?我是否錯誤地爲我的數組分配了Memeory?我想要得到它,因此輸出爲:

-> Created ArrayList of size 10 
(empty list) 
apple 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
(empty list) 
+0

不是主要的錯誤,但你應該分配length * sizeof(char *),而不是length * sizeof(int *)。 – jarmod

回答

1

以及針對ArrayList分配內存,您還需要爲每個字符串分配存儲空間。如果要設置數組的第二個元素,你可以做到這一點使用類似

void insert_at(ArrayList* arraylist, const char* str, int index) 
{ 
    arraylist->array[index] = malloc(strlen(str)+1); 
    if (arraylist->array[index] != NULL) { 
     strcpy(arraylist->array[index], str); 
    } 
} 

,並調用它像

insert_at(n, 1, "apple"); 

順便說一句,你的代碼一樣

n->array = (char **)malloc(length * sizeof(int*)); 

應該是

n->array = malloc(length * sizeof(char*)); 

(它的指針到char而非int和用C,你不應該從malloc轉換返回數組)

+0

或簡單地arraylist-> array [index] = strdup(str)。 – jarmod

+0

@jarmod是的,如果'strdup'可用。 (它是Posix的一部分,但不是標準C的一部分) – simonc

+0

謝謝你,現在已經很清楚了。它並不像在我看到的strcpy聲明中那樣簡單。 –

0

3問題

1)小:更改

n->array = (char **)malloc(length * sizeof(int*)); 

n->array = (char **)malloc(length * sizeof(char*)); 

2)把你的if (n->array == NULL)直接測試n->array = (char **)malloc(...

n->array = (char **)malloc(DEFAULT_INIT_LEN * sizeof(int*)); 
if (n->array == NULL) 
    panic("ERROR: out of memory in Mylist!\n"); 

3)最重要的。如果不先分配內存n->array[1],則不能strcpy(n->array[1], "apple");。類似於

n->array[1] = malloc(strlen("Fred")+1); 
strcpy(n->array[1], "Fred"); 
+0

感謝提示,我看到我必須動態分配兩個維度嗎? –

+0

是的,1是指針數組,然後是'長度'分配,每個字符串1。不錯,你把測試像'if(p == NULL)...' – chux

相關問題