in the past, struct variables were not directly assigned values. Today, when my colleague looked at others' code, he found that there were two statements that directly assigned values to struct variables that felt wrong at the time, it is considered that there are both general unsigned shaping and arrays in a struct, because the assignment of arrays cannot be directly assigned, and the memory copy is required. Therefore, the statement is incorrect. So I asked, I thought it was the assignment of the struct pointer variable and thought it was okay. I also argued with my colleagues. When he showed me the source code, he found that the values were directly assigned between variables. The subconscious judgment was wrong, and the reason was the same as he had previously understood. So I checked it on the internet at night and said yes, because it is the same data type, and the C language directly copies the memory when assigning values between variables of the same type, their memory will be replicated. Here, because the same struct variable belongs to the same variable, the value is directly copied according to their memory distribution. At the same time, I tested a simple code to see why. This is indeed the case. How does the C language assign values between variables? Search again and find that a variable name in C is actually an address corresponding to the starting address of a piece of memory, and the value assignment operator is to find the starting address of this memory. Audio typedef struct {unsigned char a; unsigned char B; unsigned char C; unsigned char d [10] ;}teststruct; int main (void) {teststruct A = {0 }; teststruct B = {0};. a = 1;. B = 2;. C = 3;. d [2] = 5; B = A; printf (". a: % d \ n. b: % d \ n. c: % d \ n. d [2]: % d \ n ",. a,. b,. c,. d [2]); printf ("B. a: % d \ n B. b: % d \ n B. C % d \ n B. d [2]: % d \ n ", B. a, B. b, B. c, B. d [2]); Return 0;} Reprinted from: http://blog.csdn.net/tietao/article/details/7257835
Misunderstanding of struct variable assignment