-1

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.

Community
  • 1
  • 1
Eumaa
  • 971
  • 2
  • 15
  • 38
  • 4
    You can't. You'll have to do something else. But you haven't explained what you actually want to achieve (the X in your [X-Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)) – juanchopanza Mar 03 '17 at 06:42
  • 2
    You don't; you can't assign NULL to a structure, and `*array` is equivalent to `array[0]`, which is of type `MyStruct`, which is not a pointer. – Jonathan Leffler Mar 03 '17 at 06:43
  • 1
    You attempt to assign NULL to the first element of array, not to the array itself. Presumably you wanted to do `array=NULL;`, but even that is not valid. What is your intention? – DYZ Mar 03 '17 at 06:44

2 Answers2

5

How do I assign array to NULL?

You can't assign an array to NULL. An array is not a pointer. It is a non-modifiable l-value which can't be left operand of assignment operator.

What you are doing in the statement *array = NULL; is equivalent to array[0] = NULL;. array[0] is of type MyStruct while NULL is used to assign a pointer type and this makes the the operands of assignment operator incompatible.

If you are trying to set all the elements of an array to 0 then you can do as

MyStruct array[10] = {0};
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 4
    Sir, it's not array, it's `*array`. :) – Sourav Ghosh Mar 03 '17 at 06:43
  • 2
    @SouravGhosh; Read the last statement of his question and then edited part of my answer. – haccks Mar 03 '17 at 06:44
  • I agree, the variable naming is confusing here, still, OP is not trying to assign to an array, anyway. :) – Sourav Ghosh Mar 03 '17 at 06:45
  • Thanks for answering. I also tried: int array[10]; *array = NULL; This compile was successful. Why can I set int array to NULL? – Eumaa Mar 03 '17 at 06:46
  • @CHEN You cannot. `*array = NULL;` on int array is roughly same as `array[0] = (int)NULL;`. You just converted null pointer to integer and stored that in the first element of the array. Your compiler should give warning at least. If it didn't, check your compiler settings to make sure warnings are enabled. – user694733 Mar 03 '17 at 07:08
  • @user694733 `NULL` is a null pointer constant, not necessarily a null pointer. It might (or might not be) defined as `0`. – M.M Mar 03 '17 at 07:20
  • @M.M True. But even if it compiles, it's in most cases wrong thing to do. `NULL` should be treated as null pointer, even if it were defined only as a `0`. – user694733 Mar 03 '17 at 08:18
2

*array produces a MyStruct type (non-pointer) which is not supposed to be compatible with NULL, a pointer type. The assignment is not valid.

That said, arrays are not assignable. They are not a valid LHS operand of assignment operator. You can assign individual elements of the array, but not the array (as in array variable name) itself.

Quoting C11, chapter §6.5.16

An assignment operator shall have a modifiable lvalue as its left operand.

and then, chapter §6.3.2.1

[...] A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261