0

I'm trying to pass a pointer to a struct to a function and then return an edited version of said pointer. These are the declarations of the structs:

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;
};

So as you can see there is a pointer to a struct within the other struct. My issue is when it comes to trying to do something with these things... I'll put my code so far, hopefully it's clear what's supposed to be happening but I'll add notes too.

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

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

    table = createTable(table, &size);

In this next bit is where the issues have risen, the idea is I want to edit the values inside table:

 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;
    }
}

It doesn't seem to like my function have the type of one of my structures among other things but I feel this is my main issue at the moment. Any help would be great.

Richard
  • 8,961
  • 3
  • 38
  • 47
MrPhooky
  • 139
  • 1
  • 8
  • 1
    `main` should return `int` and **not** `void` – Mohit Jain May 02 '14 at 02:38
  • 3
    You already typedef `HashTable` as a pointer, so what is `table = (HashTable*) malloc (sizeof(HashTable));` all about? It should be `HashTable table; table = malloc(sizeof *table);` Even better, don't hide the pointer with a `typedef` in the first place, and you'll avoid all these problems. – Crowman May 02 '14 at 02:40
  • Just get rid of the typedef. As is often the case, it is obscuring what is happening and causing more confusion instead of clarifying things. – William Pursell May 02 '14 at 02:48
  • 1
    You also never `malloc()` any memory for your struct's `Array` member, so that's going to end in tears, too. – Crowman May 02 '14 at 02:52
  • @PaulGriffiths, I tried adding in your edit without the typecast of (HashTable) however that returns an error saying I can't assign a value of type (void *) to something of type (HashTable). Also where I have put the function `HashTable createTable(HashTable table, int *size){` it insists that this is not a function header and that I need to put one in... – MrPhooky May 02 '14 at 09:19
  • @user3594782: The first error probably means you're trying to compile it as C++, or using MS Visual C++. I'd have to see the code for the second, but I'm suspecting a stray semi-colon. – Crowman May 02 '14 at 11:34

1 Answers1

-2

Try

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

Just modify the object passed as a parameter. You don't need a return then.

For C do

void createTable(HashTable *table, int *size){
user3344003
  • 20,574
  • 3
  • 26
  • 62