[Study Notes] [C Language] structure, Study Notes C Language

Source: Internet
Author: User

[Study Notes] [C Language] structure, Study Notes C Language

1. Three Methods for defining struct Variables
1> first define the type, then define the variable (separate definition)
Struct Student
{
Int age;
};
Struct Student stu;
 
2> define both types and variables
Struct Student
{
Int age;
} Stu;
Struct Student stu2;
 
3> define a variable while defining the type (the type name is omitted)
Struct
{
Int age;
} Stu;
 
2. struct type Scope
1> defined outside the function: globally valid (starting from the row of the definition type to the end of the file)
2> defined within a function (code block): the code block is partially valid (starting from the row of the definition type until the end of the code block)

3. Code

1> struct

1/* 2 array: it can only consist of multiple data of the same type 3 4 struct: It can consist of multiple different types of data 5 */6 # include <stdio. h> 7 8 int main () 9 {10 // int ages [3] = {[2] = 10, 11, 27 }; 11 12 13 // int ages [3] = {10, 11, 29}; 14 15 // 1. define the struct type 16 struct Person17 {// The three variables in it, which can be called the struct member or attribute 18 int age; // age 19 double height; // height 20 char * name; // name 21}; 22 23 // 2. according to the struct type, the struct variable 24 struct Person p = {20, 1.55, "jack"}; 25 p. age = 30; 26 p. name = "rose"; 27 28 printf ("age = % d, name = % s, height = % f \ n", p. age, p. name, p. height); 29 30/* incorrect syntax 31 struct Person p2; 32 p2 = {30, 1.67, "jake"}; 33 */34 35 struct Person p2 = {. height = 1.78 ,. name = "jim ",. age = 30}; 36 // p2.age = 25; 37 38 return 0; 39}

2> structure memory Analysis

1 # include <stdio. h> 2 int main () 3 {4 5 6 return 0; 7} 8 9 // Complement Algorithm 10 void test1 () 11 {12 struct Student13 {14 int age; // 4 bytes 15 16 char a; 17 18 // char * name; // 8 bytes 19}; 20 21 struct Student stu; 22 // stu. age = 20; 23 // stu. name = "jack"; 24 // complement the algorithm (alignment algorithm) 25 // the storage space occupied by the struct must be a multiple of the maximum number of member bytes 26 27 int s = sizeof (stu); 28 printf ("% d \ n", s ); 29} 30 31 // struct memory details 32 void test () 33 {34 // 1. define struct type (no storage space is allocated) 35 struct Date36 {37 int year; 38 int month; 39 int day; 40}; 41 42 // 2. define struct variables (actually allocating buckets) 43 struct Date d1 = {2011, 4, 10}; 44 45 46 struct Date d2 = {2012, 8, 9 }; 47 48 // values of all d1 members are assigned to all d2 members 49 d2 = d1; 50 d2.year = 2010; 51 52 printf ("% d-% d \ n", d1.year, d1.month, d1.day ); 53 54 printf ("% d-% d \ n", d2.year, d2.month, d2.day ); 55/* 56 printf ("% p-% p \ n", & d1.year, & d1.month, & d1.day); 57 58 int s = sizeof (d1 ); 59 printf ("% d \ n", s); 60 61 */62}

3> Notes

1 # include <stdio. h> 2 // from the beginning of this line to the end of the file, it is valid (like the global variable) 3 struct Date 4 {5 int year; 6 int month; 7 int day; 8}; 9 10 int a; 11 12 void test2 () 13 {14 struct Date 15 {16 int year; 17 }; 18 // here we use the struct Date type 19 struct Date d1 = {2011} inside the test2 function; 20 21 22 // The struct type is also available, from the row that defines the type to the end of the code block 23 struct Person 24 {25 int age; 26}; 27 28 struct Person p; 29 30 a = 10; 31} 32 33 int main () 34 {35 struct Date d1 = {2009, 8, 9}; 36 37 38 test2 (); 39 40 // The test2 function cannot be used to define the type 41 // struct Person p2; 42 43 return 0; 44} 45 46 // define the struct variable 47 void test () 48 {49 // 3rd methods for defining struct variables 50 struct {51 int age; 52 char * name; 53} stu; 54 55 struct {56 int age; 57 char * name; 58} stu2; 59 60 61/* struct type cannot be repeatedly defined 62 struct Student 63 {64 int age; 65 }; 66 67 struct Student 68 {69 double height; 70}; 71 72 struct Student stu; 73 */74 75/* incorrect syntax: repeated struct types 76 struct Student 77 {78 int age; 79 double height; 80 char * name; 81} stu; 82 83 struct Student 84 {85 int age; 86 double height; 87 char * name; 88} stu2; c 89 */90 91/* 92 this Code does two things 93 1. define struct type 94 2. use the newly defined type to define the struct variable 95 */96 // 2nd of the variables: define the type and define the variable 97/* 98 struct Student 99 {100 int age; 101 double height; 102 char * name; 103} stu; 104 105 struct Student stu2; 106 */107 108/* 109 // 1st ways to define variables: 110 // 1. type 111 struct Student112 {113 int age; 114 double height; 115 char * name; 116}; 117 118 // 2. variable 119 struct Student stu = {20, 1.78, "jack"}; 120 */121}

 

 

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.