1

I get this error error: assignment to expression with array typ when I try this:

clients[0].data->bottle = x->bottle;

This is my code:

typedef struct {
    int id;
    some_data x;
    uint64_t weight;
    some_other_data_t *data;
} CLIENTS;

typedef struct {
    uint8_t bottle[10];
} some_other_data_t;

some_other_data_t x; 
extern CLIENTS clients[5];

/* some functions that set x correctly... */

clients[0].id = 1;                   // works fine!
clients[0].data->bottle = x->bottle; // does not work :(

Why is it so easy to set the id but so hard to set the data ? I even tried to cast data to char* and use strcpy to set data but it doesn't work... How can I set data?

I googled a lot but I didnt find a solution...

menthos
  • 19
  • 3
  • array, unlike a pointer is a physically bound datatype. So you can _fill_ an array, not reassign it, it's been already assigned – user3159253 Oct 17 '17 at 11:02
  • 1
    So you may use functions like [memcpy](http://www.manpagez.com/man/3/memcpy/) to copy content of `x->bottle` to `data->bottle` – user3159253 Oct 17 '17 at 11:04
  • @user3159253 pointers are the same; the difference is that assignment operator works for pointers but not arrays – M.M Oct 17 '17 at 11:06
  • The line above doesn't appear in your code below. And the code below would give different compilation errors than the one you gave (e.g. you are using `x` like a pointer when it is a struct). – interjay Oct 17 '17 at 11:07
  • @M.M perhaps I didn't expressed it correctly. I mean: arrays have allocated fixed space and this space can't be "assigned" at once, only "filled" in cycle, while pointers, well, point to an arbitrary location and thus can be reassigned to any other location. – user3159253 Oct 17 '17 at 11:14
  • @interjay I corrected the code snippet. it is now in the code – menthos Oct 17 '17 at 11:18
  • This code still wouldn't cause this error but another unrelated one. – interjay Oct 17 '17 at 11:22
  • I get a memory access error (german: Speicherzugriffsfehler) when I use memcpy like this: memcpy(clients[0].data->bottle, x->bottle, sizeof(x->bottle)); – menthos Oct 17 '17 at 11:39
  • @interjay what do you mean ? – menthos Oct 17 '17 at 11:40
  • I mean exactly what I said: You obviously didn't test this code before posting it. – interjay Oct 17 '17 at 11:47
  • @user3159253 any variable - pointer, array, integer etc. always has the same location for its lifetime. And for all of them, you can change the stored value. Arrays use different syntax for changing the stored value but the basic fact is the same. The stored value of a pointer is the address of another object. (Avoid the mistake of thinking of the stored value of a pointer as being the value stored in the other object). – M.M Oct 17 '17 at 14:56

0 Answers0