Direct assignment of struct in C Language

Source: Internet
Author: User

I always remember that in C language, struct values cannot be directly assigned. I asked three students that they can be used in C ++, but not in C. They need to assign values to members one by one or use the memcpy function.
I tested the following program:
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
 
Struct test
{
Int;
Int B;
Char ss [10];
};
 
Int main ()
{
Struct test t1 = {1, 2, "hello "};
 
Struct test t2;
T2 = t1;
Printf ("% d, % d, % s \ n", t2.a, t2. B, t2.ss );
Return 0;
}


The answer is that you can assign values directly.
I read the following on the Internet and found a discussion post with some errors or correct discussions: http://topic.csdn.net/u/20070627/15/a6b39dfa-2ab9-473e-96c3-4054800cb4c1.html
However, if a correction member is allocated dynamically and points to a memory, pay attention to it when assigning values. You cannot simply assign values directly.
 


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.