C Language Learning Diary 5

Source: Internet
Author: User

Again looked at the super body, the most impressive is the scene of the Black class, really I imagined the campus, the heart of the academic.

1. Structural body

Without structs, in C, the organization of data depends on: variable + array. At its simplest, you only need to define a single variable using the basic data type, and you need several variables to define a few. Later the situation became complicated, and something needed a lot of meaning-related variables, when the array appeared.  The array solves the problem of requiring many types of the same, meaning-related variables. But there are limits to arrays. The biggest disadvantage of arrays is that an array can store only a number of variables with the same data type. (for example, use a data structure to hold all of a student's information: name, school number, gender.) )

What is a struct? A struct is actually a collection that contains a number of elements that have the same data type and can be different. So the structure is a kind of data encapsulation method. The meaning of the existence of a struct is that it encapsulates a large number of variables of different data types to form a big new data type.

Data structure: To organize and manage the large and complex information in a certain way, easy to operate (find, add, delete, etc.) this is called the data structure.

#include <stdio.h>
Ps:
1. The definition of struct type is outside the function, not Lim
2. The struct defines a new combination type, not a variable, and does not consume memory

struct student
{
Char name[20];//student Name
unsigned int num;//No.
int male;//Sex
}; Be careful of, not less

int main ()
{
struct student s1;//s1 is a variable, type is struct student, defined, non-initialized
Assigning values to struct variables
s1.name[0]= ' t ';
s1.name[1]= ' h ';
s1.name[2]= ' O ';
S1.name[3]= ' R ';
s1.name[4]= ' + ';
S1.num=1;
s1.male=0;
printf ("s1.name=%s\n", s1.name);
printf ("s1.num=%d\n", s1.num);
printf ("s1.male=%d\n", S1.male);
printf ("\ n");
return 0;
}

struct and array associations: arrays are a special kind of struct, and the special thing is that each element type within the package is the same. Structs and arrays all lose the encapsulation of some child elements, so the definition is encapsulated as a whole, but when used, the child elements in the package are used. General struct variables and array variables are not manipulated as a whole.

To use the struct body:

The first step: define the struct type. The definition of a struct type is outside the function.

Step two: Define the struct variables using the type defined in the first step.

Step three: Use variables. When you actually use struct variables, you use the individual child elements encapsulated in the struct variables, not the struct variables themselves.

Definition of structural body

struct student
{
Char A;
int b;
float C;
Double D;
};

int main ()
{
struct Student s=
{
. A = 1,
. B = 2,
. C = 3,
The. D = 4,//c++ extension is defined in a way that is a dream-level method that you want to define who defines who can skip elements
} ;

struct student s0={1,2,3,4}; fully initialized, successively initialized.

}

2. Common Body Union

Union is very similar in definition and use form and struct. But the two kinds of data structures are completely different from two kinds of things.

structure is the combination and encapsulation of multiple data.

There is only one thing in the common body, but it is shared by several names (and types).

#include <stdio.h>
struct male
{
Char name[20];//boys. Name. Age. Basketball class results.
Intage
int basketball;
};

struct female
{
Char name[20];//schoolgirl. Name. Age. Aerobics performance.
int age;
int aerobics;
};

Union student
{
struct male m;//use. m when male, male structure. With. f Schoolgirl, female structure
struct female F;
};

int main (void)
{
Union student U1;
U1.f.aerobics = 100;
printf ("u1.m.basketball=%d\nu1.f.aerobics=%d\n", u1.m.basketball,u1.f.aerobics);
return 0;
}

3. Macro definition

#define N 123//macro-defined format

PS:1. Macro definitions are generally outside the function 2. The macro definition must be defined before the macro is used. 3. Macro names are generally capitalized

C Language Learning Diary 5

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.