【发布时间】:2014-09-14 04:18:46
【问题描述】:
在处理输入文件之前,如何在不知道有多少字符串的情况下创建唯一字符串数组?最多可以有 200 万个字符串,最大长度为 50。 我的程序是这样的。这适用于 51 个项目,然后覆盖其他数据。如果可能的话,我不知道如何向数组中添加元素。
main() {
char *DB_NAMES[51]; // i thought this gave me ptrs to chunks of 51
// but it's 51 pointers!
char *word;
while not eof {
...function to read big string
...function to separate big sting into words
...
processWord(ctr, DB_NAMES, word);
...
}
}
processWord(int ndx, char *array1[], char *word){
...function to find if word already exists...
//if word is new, store in array
array1[ndx]= (char *)malloc(sizeof(51)); // isn't this giving me a char[51]?
strcpy(array1[ndx],word);
...
}
【问题讨论】:
-
C 中没有未知大小的数组。您必须编写自己的数据结构,如链表。
-
array1[ndx]= (char *)malloc(sizeof(51));是的,这将为array1[]分配 51 个字节的内存,但我认为大小是ndx的值