1

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)
            {
                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;
}
3
  • After a quick look, the code seems valid, though it's hard enough to spot errors in code even when the compilers do tell you which line it's on. On what line does it fail? Commented Feb 3, 2011 at 14:55
  • 1
    Please provide a code sample that is as close to compilable as possible, or indicate where the error occurs. I assume TblPTR is a typedef for Tbl*, but you didn't supply the definition of Tbl. Commented Feb 3, 2011 at 14:57
  • return statment "return from incompatible pointer type" "function returns address of local variable" Commented Feb 3, 2011 at 14:58

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You must mean: hashArray is stack-allocated within createHashTBL.
AH! :) What would the type be for the return pointer exactly? Cause I'm honestly not sure how you would do that.
Assuming you fix your memory allocation problem, you can just return a TblPTR object to the first element in your array, like this: return hashArray
2

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;

Comments

0

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)

Comments

0

You have two major problems:

  1. The type of the expression &hashArray is TblPTR (*)[size] (pointer to size-element array of TblPTR), not TblPTR; that's where your type mismatch warning comes from. However, ...

  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.