Definition and use of analytic structure _c language

Source: Internet
Author: User

Definition of structure
The general form of defining a structure is:
struct structure name
{
Member table columns
}
A member table consists of several members, each of which is an integral part of the structure.
A type description must also be made for each member.
For example:

Copy Code code as follows:

struct STU
{
int num;
Char name[20];
int age;
}

description of struct type variable
A struct definition does not define a variable, but defines a Data Type, this type is defined by you, and it can be used in the same way as a simple data type that is owned by the language itself (such as int).
The structure itself will not be used as data to open up memory, what is actually stored in memory as data is the variable defined by this structure.
How much memory space does a struct variable occupy? This is determined by the definition of this type of struct, which can be imagined in order to store each member of the structure at the same time, then the storage size of the structure variables should be the sum of the storage space of all components .。
The following three methods are used to illustrate structural variables. Take the Stu defined above as an example to illustrate.
1. Define the structure first, then describe the structure variables. As:
Copy Code code as follows:

struct STU
{
int num;
Char name[20];
int age;
};
struct Stu Boy1,boy2;

The two variables boy1 and boy2 are described as Stu struct types.
You can also use a macro definition to make a symbolic constant to represent a struct type, for example:
Copy Code code as follows:

#define STU struct STU
STU
{
int num;
Char name[20];
int age;
};
STU Boy1,boy2;

2. Describe the structure variables while defining the type of structure. For example:
Copy Code code as follows:

struct STU
{
int num;
Char name[20];
int age;
}boy1,boy2;

3. Direct description of structural variables.
For example:
Copy Code code as follows:

struct
{
int num;
Char name[20];
int age;
}boy1,boy2;

The third method differs from the second method in that the structure name is omitted from the third method, and the structure variable is given directly.
After you have explained that the Boy1,boy2 variable is a Stu type, you can assign values to each member of the two variables.
In the above Stu structure definition, all members are basic data types or array types. A member can also be a struct, which constitutes a nested structure.
For example:
Copy Code code as follows:

struct date{
int month;
int day;
int year;
}
struct{
int num;
Char name[20];
struct date birthday;
}boy1,boy2;

The general form of using struct variable members is:
Structure variable name. Member Name
For example:
Boy1.num
/* That is the first person's school number * *
If the member itself is a struct, you must find the lowest member in order to use it.
For example: Boy1.birthday.month
The month in which the first person is born can be used separately in a program, exactly the same as a normal variable.
initialization of struct variables: similar to the initialization of multidimensional arrays. Assignment of structure variables
You can assign values to the members of a struct variable in a way that is similar to an array, which is assigned one-by-element (for arrays, unless initialized, you have no choice but to assign values). Unlike arrays, struct variables in standard C can be assigned to a whole value。
Example one:
Copy Code code as follows:

#include <stdio.h>
int main (void)
{
struct student
{
long int num;
int age;
char* name;
}st1={200,18, "Zhangsan"};
struct student st2,st3;
printf ("NO. Age name/n ");
printf ("%ld%d%s/n", st1.num,st1.age,st1.name);
Getch ();
st2.num=199;
st2.age=19;
St2.name= "Lisi";
printf ("%ld%d%s/n", st2.num,st2.age,st2.name);
Getch ();
St3=st2;
printf ("%ld%d%s/n", st3.num,st3.age,st3.name);
Getch ();
printf ("/n/n struct student:%d", sizeof (struct student));
Getch ();
return 0;
}

Case TWO:
Copy Code code as follows:

#include <stdio.h>
#include <conio.h>
struct birth
{
int year;
int month;
int day;
};
struct student
{
long int num;
struct birth birthday;
char* name;
}st1={200,{1988,8,8}, "Zhangsan"};
int main (void)
{
struct student st2;
St2=st1;
printf ("%ld%s%d/n", st2.num,st2.name,sizeof (int));
printf ("Year:%d Month:%d month:%d/n",
St2.birthday.year,
St2.birthday.month,
St2.birthday.day);
Getch ();
return 0;
}

Attention
When you have a member of more than one character in your struct variable, it is recommended that you define it as an array (such as the previous name member, not knowing how large the array should be, or you can define it as a pointer). The reason is that the pointer variable cannot hold the actual data, just the address.

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.