1

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?

10
  • 2
    type of hashTable[i] is RECORD, not pointer. E.g Change to like hashTable[i] = (RECORD){ .link = NULL}; Commented Nov 5, 2017 at 10:16
  • So, can't we assing null to a struct? @BLUEPIXY Commented Nov 5, 2017 at 10:17
  • Assigning NULL to a structure has no meaning. Initialize each member. Also In your approach you can not actually set the initial value to hashTable of main. Commented Nov 5, 2017 at 10:19
  • 3
    You are also probably going to have trouble with the link field. It has type struct RECORD, but that has never been defined. When you try to access it, you will likely have trouble. Commented Nov 5, 2017 at 10:21
  • 1
    You will then run into the next issue, as the value of hashTable will be lost when returning from initializeTable. Commented Nov 5, 2017 at 10:25

1 Answer 1

2

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.

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

1 Comment

That is one of the problems. The other is the attempt to assign NULL to a structure (not a structure pointer). That is the error the OP has encountered first.

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.