Assignment of character Arrays

Source: Internet
Author: User


1: Define character arrays in struct and assign values

First look at a piece of wrongCode

Struct stype {
Char c1 [16]

Struct stype S1;
S1.c1 = "hello"; // Error

This is a naive concept error. Char c2 [10] declares an array of 10 elements C2. At this time, C2 space has been allocated. C2 is the address of this array and is a constant, if C2 = "hello" is performed again, the system tries to change C2. This is wrong.

Strcpy (s1.c1, "hello"); // This is correct


The following is a better solution:

Struct stype {
Char c1 [16]
} S1 = {"hello"}; // not only space is allocated to S1, but an initial value is also set for S1 members. You can view the c99 standard.



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: 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.