1

I am new to C programming and I was trying to implement typedef and hashing in my code.But I am getting compilation error when I try to allocate memory -

This is my header file

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

This my main code-

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

This is the error I am getting-

In function ‘main’:
hashOps.c:79:13: error: storage size of ‘h’ isn’t known
   hashTable h;
Noober
  • 1,516
  • 4
  • 22
  • 48
  • `typedef struct FirstLevelHashTable hashTable;` Should this be `typedef FirstLevelHashTable hashTable;`? – user1969104 Mar 20 '15 at 08:23
  • That gives me this error- error: request for member ‘FirstLevelHashTable’ in something not a structure or union H. FirstLevelHashTable[i]=(secLevelHashTable *)malloc(sizeof(secLevelHashTable)); – Noober Mar 20 '15 at 08:24
  • @user1969104 If I do that how can I access items of all the structs? – Noober Mar 20 '15 at 08:25
  • possible duplicate of [incompatible implicit declaration of built-in function ‘malloc’](http://stackoverflow.com/questions/7050798/incompatible-implicit-declaration-of-built-in-function-malloc) – Anto Jurković Mar 20 '15 at 08:28
  • You forgot to include ``. – Anto Jurković Mar 20 '15 at 08:29
  • @AntoJurković Thanks that fixed my warning but still I am getting the same error- hashOps.c: In function ‘main’: hashOps.c:80:13: error: storage size of ‘h’ isn’t known hashTable h; – Noober Mar 20 '15 at 08:32
  • Why didn't you post the file with the error in it????? – user253751 Mar 20 '15 at 08:36
  • I think he named his `main` file `hashOps.c`. – Matt Ko Mar 20 '15 at 08:40
  • Yes the main file is `hashOps.c` and header file is `hashDef.h` – Noober Mar 20 '15 at 08:42

1 Answers1

1

Fixed code below. It had numerous small issues and a big one.

Please read the related article the big one:

struct in C: Error storage size of 'params' isn't known -- this will explain "storage size unknown" error; by saying typedef struct FirstLevelHashTable hashTable; you were defining an unfinished struct, rather than referring to existing type.

Header file:

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

Main code:

#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;
}
Community
  • 1
  • 1
morfizm
  • 633
  • 3
  • 12
  • is there no way I can use this- `typedef Cell secLevelHashTable[MAX2];` `typedef secLevelHashTable *FirstLevelHashTable[MAX1];` – Noober Mar 20 '15 at 08:48
  • Array types are really arrays, but array names decay to pointers in specific cases: http://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers – a3f Mar 20 '15 at 08:52
  • @a3f yep, sorry. I somehow thought it's only in C++. I'll edit my answer. – morfizm Mar 20 '15 at 08:54