Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why struct assignment works with arrays in structs

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?

like image 273
gettingSmarter Avatar asked Mar 03 '16 01:03

gettingSmarter


People also ask

Is it possible to have a struct with arrays?

A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure.

Why use a struct over an array?

struct has other advantages over array which can make it more powerful. For example, its ability to encapsulate multiple data types. If you are passing this information between many functions, a structure is likely more practical (because there is no need to pass the size).

What is struct how is it different from an array discuss?

Structure can be defined as a data structure used as container which can hold variables of different types. On other hand Array is a type of data structure used as container which can hold variables of same type and do not support multiple data type variables.


1 Answers

Why after years of C programming am I having an existential language crisis about a mechanism I have used but never understood?

You always misunderstood arrays and now this has brought it to light :)

The actual rules are:

  1. Arrays are different to pointers; there is no "implied pointer" or anything in an array. The storage in memory for an array consists of exactly the cells with the array contents and nothing more.

  2. When you use the array's identifier in an expression, then the value of that expression is a (temporary) pointer to the array's first element. (With a handful of exceptions that I omit for brevity).

    2a. (in case this was unclear) Expressions have values , and the value of an expression does not require storage. For example in the code f(1 + 1), the value 2 is a value but it is not in an object and, conceptually, it is not stored anywhere. The pointer mentioned above is the same sort of value.

The reason you cannot write:

data2 = data;

is because Rule 2 kicks in , the value of the right-hand side is a pointer, and the assignment operation is not defined between an array and a pointer. (It wouldn't know how many units to copy).

The language designers could have added another exception to Rule 2 so that if the array is the sole right-hand operand of = then value conversion doesn't occur, and the array is assigned by value. That would be a consistent rule and the language would work. But they didn't.

The structure assignment does not trigger Rule 2 so the array is happily copied.

In fact they could have done away with Rule 2 entirely, and the language would still have worked. But then you would need to write puts(&s[0]); instead of puts(s); and so on. When designing C (incorporating BCPL which I think had a similar rule) , they opted to go for including Rule 2, presumably because the benefits appeared to outweigh the negatives at the time.

like image 69
M.M Avatar answered Oct 01 '22 20:10

M.M