Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C中的函数返回动态分配的结构数组?_C_Arrays_Dynamic_Struct_Hashtable - Fatal编程技术网

从C中的函数返回动态分配的结构数组?

从C中的函数返回动态分配的结构数组?,c,arrays,dynamic,struct,hashtable,C,Arrays,Dynamic,Struct,Hashtable,htable根本不像数组。有没有办法在不改变函数返回值和函数参数的情况下解决这个问题?这是学校作业的一部分,只能更改函数createHashTable的内容。程序的其余部分不在这里,因为它与问题无关。哈希表本身不应该“像数组一样工作”,这是: HashTable htable = createHashTable(tableSize); 没有意义,它返回哈希表数组中的第一个元素 不过,您不应该创建一个哈希表数组,而是应该创建一个哈希表,其中可能包含一个数组(即表)。例如,它也有一个大小变量,因此

htable
根本不像数组。有没有办法在不改变函数返回值和函数参数的情况下解决这个问题?这是学校作业的一部分,只能更改函数
createHashTable
的内容。程序的其余部分不在这里,因为它与问题无关。

哈希表本身不应该“像数组一样工作”,这是:

HashTable htable = createHashTable(tableSize);
没有意义,它返回哈希表数组中的第一个元素

不过,您不应该创建一个哈希表数组,而是应该创建一个哈希表,其中可能包含一个数组(即表)。例如,它也有一个大小变量,因此哈希表中除了数组本身之外还有其他内容

你应该这样做

return *htable;
要分配单个实例,请根据需要初始化并返回它。

您可能需要:

htable = malloc(sizeof *htable);
HashTable *htable = createHashTable(100);
...
... // when done you need to delete the hashtable
deleteHashTable(htable);
你需要这个:

HashTable htable = createHashTable(tableSize);
deleteHashTable
尚未编写,它基本上需要
free
table指针和指向
free
表本身

现在,如果您确实只允许更改
createHashTable
函数的内容,而不允许更改函数签名,那么您的问题就没有意义,因为使用函数签名
HashTable createHashTable(unsigned int size)
只能返回一个
哈希表
,但不能返回
哈希表
的数组

但也许你真的想要这个:

htable = malloc(sizeof *htable);
HashTable *htable = createHashTable(100);
...
... // when done you need to delete the hashtable
deleteHashTable(htable);
HashTable创建HashTable(无符号整数大小)
{
哈希表htable={0};
int i;
对于(i=0;i电流=NULL;
htable[i].表格->表头=NULL;
htable[i]。表->上一页=NULL;
htable[i]。表->尾=NULL;
htable[i]。大小=大小;
}
返回htable;
}

对于第二种解决方案,您仍然需要编写删除哈希表的函数。

这里似乎有一些混淆:
createHashTable()
不应该分配哈希表数组,而是一个
HashTable
结构,其嵌入的
table
成员具有初始大小

此外,按值返回结构是非标准做法。您应该返回指向已分配的
哈希表
的指针,或者可能获取指向调用者动态或静态分配的
哈希表
结构的指针,并对其进行初始化

以下是此方法代码的修改版本:

HashTable createHashTable(unsigned int size)
{
    HashTable htable = { 0 };

    int i;
    for(i=0; i<size; i++)
    {
        htable[i].table = malloc(sizeof(List));
        htable[i].table->current = NULL;
        htable[i].table->head = NULL;
        htable[i].table->prev = NULL;
        htable[i].table->tail = NULL;
        htable[i].size = size;
    }

    return htable;
}

也许你应该从函数中返回一个指向
哈希表的指针
?现在你只返回你创建的数组中的第一个元素(并且有内存泄漏)。或者将类型别名
哈希表
更改为指针(我真的不建议这样做)。分配一个哈希表数组实际上没有意义。
#include <stdlib.h>

typedef struct {
    List *table;
    unsigned int size;
} HashTable;

typedef struct node {
    Data data;
    struct node *next;
} NODE;

struct listptrs {
    NODE *tail;
    NODE *head;
    NODE *prev;
    NODE *current;
};

typedef struct listptrs List;

HashTable *createHashTable(unsigned int size) {
    HashTable *htable = malloc(sizeof(*htable));

    if (htable == NULL) 
        return NULL;
    }
    htable->size = size;
    htable->table = NULL;
    if (size == 0) {
        return htable;
    }
    htable->table = malloc(sizeof(*htable->table) * size);
    if (htable->table == NULL) {
        free(htable);
        return NULL;
    }
    for (unsigned int i = 0; i < size; i++) {
        htable->table[i].head = NULL;
        htable->table[i].tail = NULL;
        htable->table[i].prev = NULL;
        htable->table[i].current = NULL;
    }
    return htable;
}
HashTable *htable = createHashTable(100);