C Language -- struct, C language --

Source: Internet
Author: User
Tags array definition

C Language -- struct, C language --
I. struct 1. Definition of struct Variables

* Struct can be composed of multiple different types of data.

* Struct Person {int age; double height; char * name}; // first defines the struct type, which has three members or attributes.

* Struct Person p = {18, 1.80, "jack"}; // define the variable based on the struct type and initialize it.

2. assign values to struct variable attributes

1> variable attribute Assignment 1

Struct Person p = {18, 1.80, "jack "};

// Modify attributes

P. age = 20;

P. height = 1.78;

P. name = "rose ";

P = {19, 1.77, "jame"}; // incorrect value assignment method. The variable can only be initialized when it is defined.

2> struct variable value assignment 2

Struct Person p2 = {. age = 19;. height = 176;. name = "tom "};

3. Structure memory Analysis

1> struct Date {int year; int month; int day}; // the code in this row only defines a struct type and no storage space is allocated.

2> struct Date d1 = {2011,4, 10}; // defines the struct variable (actually allocating storage space ), the storage space of struct variables is the sum of all its variables-12 bytes, which can be detected by sizeof (d ).

3> struct Student {int age; char * name };

Struct Student stu = {18, "jary"}; // The struct variable occupies 16 bytes. Because when the struct volume changes to different types of data, the storage space is a multiple of the maximum types of data bytes.

4. Multiple definitions of struct Variables

1> struct Person {int age; double height; char * name };

Struct Person p = {18, 1.80, "jack "};

2> struct Person {int age; double height; char * name} p1; // defines the variable while defining the struct

Struct Person {int age; double height; char * name} p2; // incorrect syntax, repeated definition of struct type

3> struct {int age; double height; char * name} p3; // disadvantage: Repeated variables cannot be defined.

5. struct Scope

* The struct type also has a scope, starting from the row that defines the type until the end of the code block.

* Global variables and local variables can be compared for definition and use.

6. struct Array

1> struct array Definition

Struct Person {int age; double height; char * name };

Struct Person names [3] = {18, 1.80, "jack" },{ 18, 1.80, "jame" },{ 18, 1.80, "tom "}};

7. pointer to struct

1> pointer Definition

Struct Person {int age; double height; char * name}; // defines the type.

Struct Person p1 = {18, 1.80, "jack"}; // defines the struct variable

Struct Person * p; // defines the pointer of the struct Person type.

P = & p1; // pointer Variable p points to the p1 variable

2> how to access struct variable attributes

① P1.age; p1.height; p1.name; // use the variable name for direct access

② (* P). age; (* p). height; (* p). name; // access by pointer

③ P-> age; p-> height; p-> name; // use the pointer arrow

8. nested struct

1> struct Date {int year; int month; int day };

Struct Student {int age; double height; struct Date birthday}; // nested struct Definition

Struct Student stu = {18,188, {, 9 }};

Ii. Summary of data types 1. Basic Data Types

1> int

① Long int, long: 8 bytes % ld

② Short int and short: 2 bytes % d and % I

③ Unsigned int and unsigned: 4 bytes % d and % I

④ Signed int and signed: 4 bytes % d and % I

2> float/double

① Float: 4 bytes % f

② Double: 8 bytes % f

3> char

① 1 byte % c, % d

② The ASCII value of the char type stored in the memory; 'A' --> 65

2. Construction type

1> array: Only data of the same type can be stored.

Definition: data type array name [number of elements]

2> struct: It can be composed of multiple different types of data.

Definition: Define type reuse type definition variable first

3. pointer type

1> variable definition: int * p;

2> indirect operation variable value, int a = 10; int * p; p = & a; * p = 20;

4. Enumeration type

1> usage: When a variable has only a few fixed values

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.