C Language Learning 014: structured data types, language learning 014

Source: Internet
Author: User

C Language Learning 014: structured data types, language learning 014
Basic usage of struct

1 # include <stdio. h> 2 3 // define the data structure 4 struct fish {5 const char * name; 6 const char * species; 7 int teeth; 8 int age; 9 }; 10 11 void catalog (struct fish f) {12 printf ("% s is a % s with % I teeth. he is % I \ n ", f. name, f. species, f. teeth, f. age); // access structure field 13} 14 15 int main () {16 // declare the structure variable 17 struct fish snappy = {"Snappy", "Piranha ", 69,4}; 18 catalog (snappy); 19 return 0; 20}

When a structure variable is assigned to another structure variable, the computer creates a new structure copy and copies each field. If a pointer exists in the structure, only the pointer value is copied.

    struct fish snappy={"Snappy","Piranha",69,4};    struct fish gnasher=snappy;

The structure can also be nested

1 # include <stdio. h> 2 3 struct preferences {4 const char * food; 5 float exercise_hours; 6}; 7 8 struct fish {9 const char * name; 10 const char * species; 11 int teeth; 12 int age; 13 struct preferences care; // struct nested in fish preferences14}; 15 16 void catalog (struct fish f) {17 printf ("% s is a % s with % I teeth. he is % I \ n ", f. name, f. species, f. teeth, f. age); // access structure field 18 // structure 19 printf ("% s like to eat % s \ n", f. name, f. care. food); 20 printf ("% s like to exercise % f hours \ n", f.name,f.care.exe rcise_hours); 21} 22 23 int main () {24 struct fish snappy = {"Snappy", "Piranha", 69,4, {"Meat", 7.5 }}; 25 catalog (snappy); 26 return 0; 27}

Create more simple Structure Variables

By using typedef to name the structure, you can save the struct keyword when creating the structure variable.

# Include <stdio. h> typedef struct cell_phone {int cell_no; const char * wallpapaer;} phone; // phone is the type name (alias of cell_phone) int main () {phone p = {5555, "sinatra.png"}; printf ("number % I \ n", p. cell_no); return 0 ;}

We can also directly Save the name definition structure of the structure, which is called the anonymous structure.

1 typedef struct {2     int cell_no;3     const char *wallpapaer;4 } phone;
Transfer Structure pointer

When a structure is assigned to another structure, we know that a new copy is created. If we want to update the original structure through the structure to be assigned, we need to use the structure pointer.

1 # include <stdio. h> 2 3 typedef struct {4 int cell_no; 5 const char * wallpapaer; 6} phone; 7 8 int main () {9 phone p = {5555, "sinatra.png "}; 10 phone p2 = p; 11 p2.cell _ no = 4444; 12 printf ("p. cell_no: % I p2.cell _ no: % I \ n ", p. cell_no, p2.cell _ no); 13 phone * p3 = & p; // assign the address of structure p to * p314 (* p3 ). cell_no = 6666; 15 printf ("p. cell_no: % I p2.cell _ no: % I p3.cell _ no: % I \ n ", p. cell_no, p2.cell _ no, (* p3 ). cell_no); 16 return 0; 17}

Because we often write (* p2). wallpapaer errors as * p2.wallpapaer, they are not equivalent. Therefore, C language developers have designed a simpler method to Represent Structure pointers.

1 int main(){2      phone p={5555,"sinatra.png"};3      phone* p2=&p;4      printf("p2->wallpapaer:%s = (*p2).wallpapaer:%s\n",p2->wallpapaer,(*p2).wallpapaer);//5     return 0;6 }

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.