Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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
Can';t成功传递/返回结构指针_C_Pointers_Struct - Fatal编程技术网

Can';t成功传递/返回结构指针

Can';t成功传递/返回结构指针,c,pointers,struct,C,Pointers,Struct,我试图将指向结构的指针传递给函数,然后返回该指针的编辑版本。 以下是结构的声明: struct HshTble; typedef struct HshTble *HashTable; enum EntryStatus { Occupied, Empty, Deleted }; struct HashEntry { int Element; enum EntryStatus Info; }; typedef struct HashEntry Entry; struct HshTb

我试图将指向结构的指针传递给函数,然后返回该指针的编辑版本。 以下是结构的声明:

struct HshTble;
typedef struct HshTble *HashTable;
enum EntryStatus { Occupied, Empty, Deleted };
struct HashEntry {
    int Element;
    enum EntryStatus Info;
};
typedef struct HashEntry Entry;

struct HshTble{
    int TableSize;
    Entry *Array;
};
如您所见,在另一个结构中有一个指向该结构的指针。 我的问题是当涉及到试图用这些东西做些什么的时候。。。我将把我的代码放在这里,希望它清楚地说明应该发生什么,但我也会添加注释

void main(){
    HashTable *table;
    int size;

    table = (HashTable*) malloc (sizeof(HashTable));

    table = createTable(table, &size);
在下一位中,问题出现了,我想编辑表中的值:

 HashTable createTable(HashTable table, int *size){
    int x, y, ch;
    *size = 0;
    fopen_s(&fp, DATA_FILENAME, "r");

    while (EOF != fscanf_s(fp, DATA_FILENAME, &ch))
    {
        y = x = f(ch);
        ++*size;

        if (table->Array[x].Info != Occupied)
        {
            table->Array[x].Element = ch;
            table->Array[x].Info = Occupied;
        }
        else if (table->Array[x].Info == Occupied)
        {
            while (table->Array[y].Info == Occupied)
            {
                if (y > HASH_TABLE_SIZE)
                {
                    y = 0;
                    table->Array[y].Element = ch;
                    table->Array[y].Info = Occupied;
                }
                else
                {
                    table->Array[y + 1].Element = ch;
                    table->Array[y + 1].Info = Occupied;
                }
                y++;
            }
        }
        return table;
    }
}
我的函数似乎不像我的结构,但我觉得这是我目前的主要问题。任何帮助都会很好。

试试看

void createTable(HashTable &table, int *size){
只需修改作为参数传递的对象。那你就不需要退货了

对于C do

void createTable(HashTable *table, int *size){

main
应该返回
int
而不是
void
您已经键入def
HashTable
作为指针,那么什么是
table=(HashTable*)malloc(sizeof(HashTable))所有关于?它应该是
哈希表;表=malloc(sizeof*表)
更好的是,首先不要用
typedef
隐藏指针,这样可以避免所有这些问题。只需去掉typedef即可。通常情况下,它会掩盖正在发生的事情,并导致更多的混乱,而不是澄清事情。你也从来没有为你的结构的
数组
成员添加过任何内存,所以这也会以眼泪告终。@PaulGriffiths,我试着在你的编辑中添加没有(哈希表)类型转换的内容但是,这会返回一个错误,表示无法将类型(void*)的值分配给类型(HashTable)的对象。另外,我把函数
放在HashTable createTable(HashTable,int*size){
的地方,它坚持认为这不是函数头,我需要放一个进去……在C中没有引用类型。对不起,我很抱歉。