Returning a pointer to an array of pointers to structs
So, I'm having a little problem. I'm trying to build a hash table, but I keep getting an error saying "return from incompatible pointer type." I know what this means, but I don't know why my code isn't working. I'm looking for an explanation of why my code does not work. Why does it not recognize the array as a pointer?
I'm making an array of pointers to structs for a hash table. (externally chained) (I know that my code probably really sucks >< I'm still learning!)
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)
开发者_JAVA百科 {
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;
}
You have at least two problems.
First, your createHashTBL()
function is defined to return a TblPTR
object, and you're returning a pointer to an array of TblPTR
objects. You should change the function type to match the return type you're trying for, or return the right type of object.
Second, your hashArray
is stack-allocated within the createHashTBL()
function, which means you can't return a pointer to it. It will go away when your function returns. You should try allocating the array with malloc()
or having the caller provide a pointer to a pre-allocated array.
TblPTR hashArray[size];
is created on the stack and cannot be returned, because your variable will be destroyed at the end of your function.
You should use malloc() instead, or static TblPTR hashArray[size];
(not recommended).
And this is wrong :
return &hashArray;
You are returning a pointer to your array : (TblPTR*). Just do
return hashArray;
Your compiler error hints that it might also be another problem too, such as a missing typedef. You should always copy/paste error messages so we can inspect them, as well as indicating which line the error is on in the code you paste - this will help everyone in understanding the problem
There's some errors here though.
TblPTR createHashTBL(int size) {
...
TblPTR hashArray[size];
..
return &hashArray;
}
- You cannot return a pointer to a local variable - that variable is gone when the function returns
- createHashTable is declared to return a TblPTR, but return &hashArray; has a completely different type, it's a pointer to an array of TblPTR.
That function should probably be
TblPTR *createHashTBL(int size) {
...
TblPTR *hashArray = malloc(size * sizeof *hashArray);
..
return hashArray;
}
(Remember to free() the elements and the hashArray when you're done with it)
You have two major problems:
The type of the expression
&hashArray
isTblPTR (*)[size]
(pointer tosize
-element array ofTblPTR
), notTblPTR
; that's where your type mismatch warning comes from. However, ...hashArray
is local to the function; as soon as the function exits,hashArray
is no longer valid, so you'll be returning a pointer to garbage.
A VLA is not the right tool to use here. I suggest making the following changes:
TblPTR *createHashArray(size) // return a pointer to TblPTR
{
...
TblPTR *hashArray = malloc(sizeof *hashArray * size);
if (hashArray)
{
// initialize hash array as you're currently doing
}
return hashArray;
}
Note that you'll have to free()
the array at some point later in your code.
精彩评论