Mawg
Mawg

Reputation: 40149

Compiler thinks struct is pointer to struct

I declare an array of structs and want to declare a second into which I can make a copy in order to re-initialze the structure at the start of each unit test.

So, I declare in 2 header files

extern peripheralsArray_t Peripherals;  
extern peripheralsArray_t DefaultPeripherals;

and in 2 c files

peripheralsArray_t Peripherals =
{... init  values };
peripheralsArray_t DefaultPeripherals;

BUT, when I try to assign DefaultPeripherals = Peripherals; the GCC comiler (under Cygwin) says

error: incompatible types when assigning 
       to type ‘peripheralsArray_t’ from type ‘struct _peripherals *’

Why does it think that Peripherals is a pointer?

Upvotes: 0

Views: 156

Answers (2)

Ed Swangren
Ed Swangren

Reputation: 124682

You cannot assign to an array. I'm assuming that peripheralsArray_t is a typedef'd array (which is almost always a bad idea as well).

Upvotes: 2

Assuming that your type names are descriptive and peripheralsArray_t really is an array.

C does not support array assignment, which is what you are trying to do.

So the compiler see a array in a RHS context and deduces that it should decay to a pointer, which then has the wrong type.

Upvotes: 2

Related Questions