我是C语言编程的新手,我试图在代码中实现typedef和哈希,但是当我尝试分配内存时遇到编译错误-
这是我的header
档案
#define MAX1 11
#define MAX2 23
typedef short IP[4];
typedef enum{TRUE = 1,FALSE = 0}boolean;
typedef struct
{
IP p;
char *comp_name;
}Element;
typedef struct
{
Element e;
boolean deleted; // deleted flag
boolean empty;
}Cell;
typedef Cell secLevelHashTable[MAX2];
typedef struct secLevelHashTable *FirstLevelHashTable[MAX1];
typedef struct FirstLevelHashTable hashTable;
这是我的main
代码-
#include"hashDef.h"
#include<stdio.h>
#include<stdlib.h>
void initFirstHTable(hashTable H)
{
int i,j;
for(i=0;i<MAX1;i++)
{
H. FirstLevelHashTable[i]=(secLevelHashTable *)malloc(sizeof(secLevelHashTable));
H.FirstLevelHashTable[i]->secLevelHashTable=malloc(sizeof(Cell)*MAX2);
for(j=0;j<MAX2;j++)
{
initSecHTables(H.FirstLevelHashTable[i]->secLevelHashTable[j]);
}
}
}
void initSecHTables(Cell *ptr)
{
ptr->deleted=0;
ptr->empty=1;
}
int main()
{
hashTable h;
h=malloc(sizeof(FirstLevelHashTable));
initFirstHTable(h);
return 0;
}
这是我得到的错误-
In function ‘main’:
hashOps.c:79:13: error: storage size of ‘h’ isn’t known
hashTable h;
下面的固定代码。它有许多小问题,而有一个大问题。
请阅读相关文章的大篇幅:
C中的struct:“参数”的错误存储大小未知-这将解释“未知的存储大小”错误;通过说typedef struct FirstLevelHashTable hashTable;
您正在定义未完成的结构,而不是引用现有的类型。
头文件:
#define MAX1 11
#define MAX2 23
typedef short IP[4];
typedef enum{TRUE = 1,FALSE = 0}boolean;
typedef struct
{
IP p;
char *comp_name;
}Element;
typedef struct
{
Element e;
boolean deleted; // deleted flag
boolean empty;
}Cell;
typedef Cell secLevelHashTable[MAX2];
typedef secLevelHashTable* FirstLevelHashTable[MAX1];
typedef FirstLevelHashTable hashTable;
主要代码:
#include"hashDef.h"
#include <stdio.h>
#include <stdlib.h>
void initSecHTables(Cell *ptr)
{
ptr->deleted=0;
ptr->empty=1;
}
void initFirstHTable(hashTable H)
{
int i,j;
for(i=0;i<MAX1;i++)
{
H[i]=(secLevelHashTable *)malloc(sizeof(secLevelHashTable));
for(j=0;j<MAX2;j++)
{
initSecHTables(&((*H[i])[j]));
}
}
}
int main()
{
hashTable h;
initFirstHTable(h);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句