C structure initialization problem, c structure Initialization

Source: Internet
Author: User

C structure initialization problem, c structure Initialization

Array initialization in struct my code

C/C ++ code # include <stdio. h> # include <stdlib. h> struct a {int length; int c [5];} B; int main () {B. c = {1, 2, 3, 4, 5 };}

 

The compiler reports an error during debugging.

------ Solution ------------------ your struct is a global variable and has been allocated space before entering main.

{} Can only be used for initialization and can only be used for definition. {} Is not allowed to assign values to arrays.

Your code should be changed like this.

C/C ++ code # include <stdio. h> # include <stdlib. h> struct a {int length; int c [5];} B = {0, 1, 2, 3, 4, 5}; // 0 is initialized to length, the last five are for the array, in order. Int main () {}------ solution -------------------- C/C ++ code # include <stdio. h> # include <stdlib. h> struct a {int length; int c [5];} B; int main () {int I; for (I = 0; I <5; I ++) b. c [I] = I + 1 ;}

 

------ Solution -------------------- refer to the initialization of a common array such as {1, 2, 3, 4, 5 }.

The allocated space can only be assigned values individually or cyclically.

The initialization of the number of character groups is the same as above ..

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.