I was attempting to explain to a co worker a concept and came to the realization I was incorrect in my understanding.
How are structs with arrays embedded assignable?
For example:
typedef struct {
uint8_t data[8];
} Test;
...
Test test1;
Test test2;
... some assignment to test1
test2 = test1;
I know if data was of type pointer that we would need to implement a deep copy but I'm trying to understand fully the way this works.
My though process is that as 'data' would normally be the pointer to the first element and that &data would be the address of that pointer. In the case of the struct is the struct address what the compiler is using to access the array?
Can someone explain the language mechanism that allows this. Is this just syntactic sugar for c structs? If so, why not implement direct array assignment like so...
uint8_t data[10];
uint8_t data2[10];
...
data2 = data;
Why after years of C programming am I having an existential language crisis about a mechanism I have used but never understood?
datais an array, NOT the pointer to the first element and is converted to a pointer to first element in expression except for operatorsizeofand unary&.&datawill be the address of array, not address of pointer._Alignas.@nameto address a comment; I just happened to still read in this thread). Anyway, please keep in mind that if an array was a pointer, it would be called "pointer", not "array". Take @MikeCAT s comment very seriously! It also is true when using the index-operator[]. There is no black magic and C is actually very straight forward here. However, why do you think an array inside astructwould behave differently than anint?