Misunderstanding of struct variable assignment

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.