E
E
elBroom2016-02-28 15:44:47
C++ / C#
elBroom, 2016-02-28 15:44:47

Why does the incompatible types when assigning to type error occur?

error:
error: incompatible types when assigning to type 'struct Item[100]' from type 'struct Item *'
table = fizz(table);
The code:

#define SIZE 100
struct Item{
  int key;
  int val;
};

struct Item * fizz(struct Item *table){
  return table;
}

int main(){
  struct Item table[SIZE];
  table = fizz(table);
}

Compiler:
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Question about C.
Why does passing table to fizz convert struct Item[100] to struct Item *table, but not assigning it? How to fix the error?
ideone.com/uBPnhE

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2016-02-28
@jcmvbkbc

table = fizz(table);

What did you want to do?
If you initialize the array with the value returned by fizz, you just had to initialize it in this function.
If you change the memory area that table points to, then you had to make table a pointer.
Because an array is the syntax for statically allocating memory on the stack, in a data area, or in structures. The array name is the name of this block of memory, and its address is the address of this block. Therefore, the array name can be easily converted to an address. But if the language made it possible to change the address of an array by assigning its name, the array, in addition to its elements, would have to contain an additional pointer. It would be a strange, complex class of objects that combines the properties of an array and a pointer. Because C is a simple language, you have separate arrays and separate pointers, and you can make your own complex type.

S
sitev_ru, 2016-02-28
@sitev_ru

cpp.sh/85gg

A
Alexander Movchan, 2016-02-28
@Alexander1705

Because you can't assign arrays. Such a standard. Think of an array as a constant pointer. It can be copied, but not modified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question