【发布时间】:2011-02-03 14:51:28
【问题描述】:
所以,我遇到了一个小问题。我正在尝试构建一个哈希表,但我不断收到一条错误消息,提示“从不兼容的指针类型返回”。我知道这意味着什么,但我不知道为什么我的代码不起作用。我正在寻找为什么我的代码不起作用的解释。为什么不将数组识别为指针?
我正在为哈希表创建一个指向结构的指针数组。 (外链) (我知道我的代码可能真的很烂>
struct hashTBL {
char *userID;
char *password;
struct hashTBL *next;
};
typedef struct hashTBL Tbl;
typedef struct hashTBL* TblPTR;
TblPTR createHashTBL(int size)
{
char *userID;
char *password;
int i;
TblPTR hashArray[size];
FILE* fpData;
char *fileName = "encrypted.txt";
fpData = openReadFile(fileName);
TblPTR T = NULL;
while((fscanf(fpData, "%s", userID)) != EOF)
{
fscanf(fpData, "%s", password);
i = hash(userID, size);
if(hashArray[i] != NULL)
{
TblPTR H = hashArray[i];
while(H != NULL)
{
T = H;
H = H->next;
}
H = newPTR(userID, password, T);
}
else
{
hashArray[i] = newPTR(userID, password, T);
}
}
closeFile(fpData);
return &hashArray;
}
TblPTR newPTR(char *userID, char *password, TblPTR T)
{
TblPTR H = (TblPTR)malloc(sizeof(Tbl));
if(T != NULL) T->next = H;
H->userID = userID;
H->password = password;
H->next = NULL;
return H;
}
【问题讨论】:
-
快速浏览后,代码似乎有效,尽管即使编译器确实告诉你它在哪一行,也很难发现代码中的错误。它在哪一行失败?
-
请提供尽可能接近可编译的代码示例,或指出错误发生的位置。我假设
TblPTR是Tbl*的typedef,但您没有提供Tbl的定义。 -
return statment "从不兼容的指针类型返回" "函数返回局部变量的地址"
标签: c pointers data-structures