JollyRoger
JollyRoger

Reputation: 847

Incompatible Type Error While Implementing a Struct Array

I am trying to create a struct array named Record. For this, I used the code below:

typedef struct{
  int studentID;
  char studentName[20];
}STUDENT;

typedef struct{
   STUDENT item;
   struct RECORD *link;
}RECORD;

void initializeTable(RECORD *, int);

int main(){
  int i;
  int m;
  RECORD *hashTable;

  printf("Table Size: "); scanf("%d", &m);
  initializeTable(hashTable, m);
}

void initializeTable(RECORD *hashTable, int m){
  int i;
  hashTable = (RECORD *)malloc(m * sizeof(RECORD));
  for(i=0; i<m; i++){
    hashTable[i] = NULL;
  }
}

I got this error:

incompatible types when assigning to type ‘RECORD {aka struct <anonymous>}’ from type ‘void *’
 hashTable[i] = NULL;

Where am I doing wrong?

Upvotes: 1

Views: 1219

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

In this typedef declaration

typedef struct{
   STUDENT item;
   struct RECORD *link;
}RECORD;

There are declared two types. The first one is the type that has typedef name RECORD. And the second one is incomplete type struct RECORD that is declared inside the type RECORD. They are two different type.

You should for example declare the structure like

typedef struct RECORD{
   STUDENT item;
   struct RECORD *link;
}RECORD; 

Also this loop

  hashTable = (RECORD *)malloc(m * sizeof(RECORD));
  for(i=0; i<m; i++){
    hashTable[i] = NULL;
  }

does not make sense because the expression hashTable[i] is not a pointer but an object of the type RECORD.

Upvotes: 2

Related Questions