Struct array initialization, struct Array

Source: Internet
Author: User

Struct array initialization, struct Array

Const int MAXN = 100; struct A {int a, B ;}; struct A arr [100]; // at this time, it is compiled through struct A arr [MAXN]; // The compilation fails at this time. Why?


Struct array Initialization

/*
# Include <stdio. h>
# Include <stdlib. h>
Struct record {
Char URL [40];
Char Title [20];
Char Keyword [16];
Double PageRank;
};
Typedef struct record Record;
Int main ()
{
Record t [2] = {"ii", "jj", "kk", 0 },{ "ii", "jj", "kk", 0 }};
Printf ("% s", t [1]. URL );
Return 0;
}*/

If you want to initialize it when defining it, it must be the same as the above. Otherwise, you must initialize it one by one, as shown in the following code.

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Struct record {
Char URL [40];
Char Title [20];
Char Keyword [16];
Double PageRank;
};
Typedef struct record Record;
Int main ()
{
Record t [2];
Strcpy (t [0]. URL, "ii ");
Strcpy (t [0]. Title, "jj ");
Strcpy (t [0]. Keyword, "kk ");
T [0]. PageRank = 0.0;
Strcpy (t [1]. URL, "ii ");
Strcpy (t [1]. Title, "jj ");
Strcpy (t [1]. Keyword, "kk ");
T [1]. PageRank = 0.0;
Printf ("% s", t [1]. URL );
Return 0;
}

C language, how to initialize struct Arrays

Method 1:
Struct student stu [100] = {0 };

Method 2:
Struct student stu [100];
Memset (stu, 0, sizeof (stu ));

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.