从C中的函数返回字符串数组

从C中的函数返回字符串数组,c,arrays,string,C,Arrays,String,我将给定文件(字典)中的单词读入单个字符串,并将该字符串分配给字符串数组的第n个索引。但它不起作用。main()中for循环的输出总是e3V\347等,而createWordTable()中for循环的输出总是字典的最后一个字。这是我的密码 char** createWordTable(); char** createTable(); int main() { int i; char **hashTable; hashTable = createTable();

我将给定文件(字典)中的单词读入单个字符串,并将该字符串分配给字符串数组的第n个索引。但它不起作用。
main()
中for循环的输出总是
e3V\347
等,而
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;
}

}`您正在丢失createTable()结果。将其存储为单独的指针变量

hashTable = createTable();
hashTable = createWordTable();

您应该将
fscanf
直接导入
表[wordcount]
strcpy
中的
word
。否则,每个条目将只指向word,其中包含文件中的最后一个字符串。

从函数返回字符串数组的选项是使用
NUL
终止字符串

此数据结构是一个字符串序列,一个字符串存储在内存中,每个字符串都以
NUL
结尾,并在末尾附加一个
NUL
-终止符,例如:

+---+---+---+---+---+-----+---+---+---+---+---+-----+-----+
| H | e | l | l | o | NUL | w | o | r | l | d | NUL | NUL |
+---+---+---+---+---+-----+---+---+---+---+---+-----+-----+
                                                 ^^^^^^
                                      Double-NUL at the end
您可以从函数返回指向第一个字符串的指针,即序列的开头

这种数据结构的一大优点是,它在数组中具有非常好的字符串局部性

从以下源代码中可以看出,此数据结构不难实现,并且易于导航:

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

#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 '\0' */
        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 = '\0';

    /* 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 != '\0'; p += (strlen(p) + 1)) {
        printf("%s\n", p);
    }

    /* Free array memory */
    free(test_string_array);

    /* All right */
    return 0;
}
#包括
#包括
#包括
#定义数组大小(a)(sizeof(a)/sizeof(a[0]))
字符*生成字符串数组(无效){
常量字符*测试字符串[]={
“你好”,
“世界”,
“嗨”,
“约翰”,
“康妮”
};
int i;
char*p;
字符*字符串_数组;
国际总工会;
/*计算字符串的总长度*/
总长度=0;
对于(i=0;i<数组大小(测试字符串);i++){
/*使用当前字符串更新总长度。+1表示“\0”*/
总长度+=strlen(测试字符串[i])+1;
}
*考虑双NUL终止*/
总长度++;
/*为生成的字符串数组分配内存*/
字符串数组=malloc(总长度);
if(字符串_数组==NULL)
返回NULL;/*错误*/
/*将源字符串复制到目标字符串数组内存*/
p=字符串_数组;
对于(i=0;i<数组大小(测试字符串);i++){
strcpy(p,test_strings[i]);
p+=(strlen(p)+1);/*+1跳过终止NUL*/
}
/*以双NUL终止*/
*p='\0';
/*将字符串数组的地址返回给调用方*/
返回字符串_数组;
}
int main(){
字符*测试字符串数组;
常量字符*p;
/*创建测试字符串数组*/
测试字符串数组=构建字符串数组();
if(测试字符串数组==NULL){
printf(“创建数组时出错。\n”);
返回1;
}
/*打印字符串数组内容*/
对于(p=测试字符串数组;*p!='\0';p+=(strlen(p)+1)){
printf(“%s\n”,p);
}
/*空闲阵列存储器*/
自由(测试字符串数组);
/*好的*/
返回0;
}

当你在一个房间里抛出C、字符串和一个没有经验的程序员时,我总是感到惊讶。我只是稍微编辑了一下你的帖子,修正了问题正文和标题中的一些格式(例如大写的“C”)。我之前做过,但我遇到了exc_bad_访问问题。我不确定这是否是一个选项。你失去了拥有空字符串的可能性…@glglgl:关于空字符串,你是对的。如果需要空字符串,那么另一个选项是在上述结构之前使用一种“头”,存储字符串指针序列,并用
NULL
指针终止它。在该标题(包含字符串指针)之后,字符串跟随。
+---+---+---+---+---+-----+---+---+---+---+---+-----+-----+
| H | e | l | l | o | NUL | w | o | r | l | d | NUL | NUL |
+---+---+---+---+---+-----+---+---+---+---+---+-----+-----+
                                                 ^^^^^^
                                      Double-NUL at the end
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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 '\0' */
        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 = '\0';

    /* 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 != '\0'; p += (strlen(p) + 1)) {
        printf("%s\n", p);
    }

    /* Free array memory */
    free(test_string_array);

    /* All right */
    return 0;
}