Why is the array passed by reference, but the structure is oddly passed by value in C?

Why are they so design? I am very confused.

Update: I really want to know the answer to the question. I am Googled, but I can not find anything useful.

I think there can be two reasons:

One : technical. Perhaps this is difficult to implement by the compiler (struct pass by reference). If the answer is this. Please explain this.

Two : useful for use (programming). As I think it is more elegant (or beautiful?), If the structure follows the link. I cannot find a few examples showing the advantage (strut pass by value). My programming career is not very long. In practice, I prefer to pass the struct function as a pointer (the pointer takes up less space on the stack).

+5
source share
2 answers

you can imagine an array as a set containing a sequential memory cell with the name of the array as a pointer to the first memory address of this set.

So, now it should be obvious that when you pass an array by its name, you actually pass a copy of the address of this memory.

, , , , , .

PS: C , C-, .

-1

, . c .

, , , ,

-, .

-, , , , . C , . , - malloc.

-2

All Articles