A question about array assignment in C

Source: Internet
Author: User

Array assignment I would like to sum up three types, the char example:

 

Assign values directly when defining.

1: Char A [20] = "Hello world! ";

 

2: Char A [20];

Strcpy (a, "Hello world! ");

 

3: Char A [20] = {'h', 'E', 'l', 'l', 'O', '', 'w', 'O ', 'R', 'l', 'D ','! '};

 

Common error Assignment Methods:

 

1: Char A [20];

A = "Hello world! ";

 

Cause: first, a char array with a length of 20 is defined, that is, 20 consecutive units are opened in the stack during runtime, and a is the first address of 20 units.

Next, heap a string "Hello world! ", Assign the landlord of this string to A, and a is the address of 20 consecutive units. So this time is messy. You cannot assign values like this.

 

Related Article

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.