I ran into an incompatible type error when I try to assign a self-constructed type array to NULL. Here is my code:
#include <stdio.h>
#incldue <stdlib.h>
typedef struct {
int i;
double j;
} MyStruct;
int main() {
MyStruct array[10];
... //doing something
*array = NULL;
return 0;
}
and I compiled it on Ubuntu using:
gcc -o main main.c
The compiler shows the following error:
error: incompatible types when assigning to type 'MyStruct {aka struct <anonymous>}' from type 'void *'
*array = NULL;
^
How do I assign array to NULL?
I know that an array and a pointer are different, and in most cases array names are converted to pointers. (as this question explains: Is an array name a pointer?) But things are a little different when my program involves self-constructed structure.
I tried the following 2 things:
// 1st
int main() {
int array[10];
... //doing something
*array = NULL; // this gives a warning!!!
return 0;
}
The code above only has a warning when I compile it, while the code below has an error.
// 2nd
int main() {
MyStruck array[10];
... //doing something
*array = NULL; // this gives an Error!!!
return 0;
}
I also want to know what makes the difference.