我从给定的文件(字典)读取单个字符串的单词,并将字符串分配给字符串数组的第n个索引。 但它不起作用。 main()
for循环的输出始终为e3V347
等,而createWordTable()
for循环的输出始终是字典的最后一个单词。 这是我的代码
char** createWordTable(); char** createTable(); int main() { int i; char **hashTable; hashTable = createTable(); hashTable = createWordTable(); for (i=0; i< 338; i++) { printf("%s n",hashTable[i]); } return 0; } char** createWordTable(){ char word[20],**table; FILE *dicFile; table = createTable(); dicFile = fopen("smallDictionary.txt", "r"); if (dicFile == NULL) { perror("error"); } int wordCount = 0,endFile = 1; while (endFile != EOF) { endFile = fscanf(dicFile,"%s",word); table[wordCount] = word; wordCount = wordCount+1; } for (int i=0; i< 338; i++) { printf("%s n",table[i]); } return table; } char** createTable(){ char **table; int i; table = (char **)malloc(338 * sizeof(char *)); for (i=0; i<=338; i++) { *table = (char *)malloc(25 * sizeof(char)); } return table; }
我将代码更改为此及其工作! 我定义了全局变量’table’并删除了指针(也是动态分配函数)。 我很好奇为什么指针不适用于C代码中的字符串数组(我知道方括号也意味着’指针’)? 因为我对整数数组没有不好的经验。 抱歉英文不好,这是新代码:`
char words [338] [10];
int main()
{
createWordTable(); for (int i=0; i< 338; i++) { printf("%s n",words[i]); } return 0;
}
void createWordTable(){
char word[20]; FILE *dicFile; dicFile = fopen("smallDictionary.txt", "r"); if (dicFile == NULL) { perror("error"); } int wordCount = 0; while (!feof(dicFile)) { fscanf(dicFile,"%s",word); if(feof(dicFile)) break; strcpy(words[wordCount], word); wordCount = wordCount+1; } fclose(dicFile);
}`
您正在丢失createTable()结果。 存储它单独的指针变量。
hashTable = createTable(); hashTable = createWordTable();
从函数返回字符串数组的选项是使用双NUL
终止字符串 。
这个数据结构是一个字符串序列,一个存储在另一个存储器中,每个NUL
终止,并在末尾有一个额外的 NUL
-terminator,例如:
+---+---+---+---+---+-----+---+---+---+---+---+-----+-----+ | H | e | l | l | o | NUL | w | o | r | l | d | NUL | NUL | +---+---+---+---+---+-----+---+---+---+---+---+-----+-----+ ^^^^^^ Double-NUL at the end
您可以从函数返回指向第一个字符串的指针,即返回到序列的开头。
这种数据结构的一大优势是它对arrays中的字符串具有非常好的局部性 。
这个数据结构并不难实现,并且易于导航 ,您可以从以下源代码中看到:
#include #include #include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) char * build_string_array(void) { const char * test_strings[] = { "Hello", "World", "Hi", "John", "Connie" }; int i; char * p; char * string_array; int total_len; /* Calculate total length of strings */ total_len = 0; for (i = 0; i < ARRAY_SIZE(test_strings); i++) { /* Update total length with current string. +1 for ' ' */ total_len += strlen(test_strings[i]) + 1; } /* Consider double-NUL termination */ total_len++; /* Allocate memory for the resulting string array */ string_array = malloc(total_len); if (string_array == NULL) return NULL; /* error */ /* Copy source strings to the destination string array memory */ p = string_array; for (i = 0; i < ARRAY_SIZE(test_strings); i++) { strcpy(p, test_strings[i]); p += (strlen(p) + 1); /* +1 to skip terminating NUL */ } /* Terminate with double-NUL */ *p = ' '; /* Return the address of the string array to the caller */ return string_array; } int main() { char * test_string_array; const char * p; /* Create the test string array */ test_string_array = build_string_array(); if (test_string_array == NULL) { printf("Error in creating array.n"); return 1; } /* Print string array content */ for (p = test_string_array; *p != ' '; p += (strlen(p) + 1)) { printf("%sn", p); } /* Free array memory */ free(test_string_array); /* All right */ return 0; }
你应该fscanf
直接进入table[wordcount]
或strcpy
从word
。 否则,每个条目都只指向word,其中包含文件中的最后一个字符串。
以上就是c/c++开发分享从C中的函数返回字符串数组相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/559699.html