C Language Learning 014: structured data types

Source: Internet
Author: User

Basic use of structs
1#include <stdio.h>2 3 //Defining data Structures4 structfish{5     Const Char*name;6     Const Char*species;7     intteeth;8     intAge ;9 };Ten  One voidCatalogstructFish f) { Aprintf"%s is a%s with%i teeth. He is%i\n", f.name,f.species,f.teeth,f.age);//access the fields of the structure - } -  the intMain () { -     //declaring struct variables -     structFish snappy={"Snappy","Piranha", the,4}; - catalog (snappy); +     return 0; -}

When you assign a struct variable to another structure variable, the computer creates a completely new copy of the structure, and then copies each of the fields, and if there is a pointer in the struct, the value of the pointer is copied only

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

Structures can also be nested using

1#include <stdio.h>2 3 structpreferences{4     Const Char*Food ;5     floatexercise_hours;6 };7 8 structfish{9     Const Char*name;Ten     Const Char*species; One     intteeth; A     intAge ; -     structPreferences care;//structure nested in fish preferences - }; the  - voidCatalogstructFish f) { -printf"%s is a%s with%i teeth. He is%i\n", f.name,f.species,f.teeth,f.age);//access the fields of the structure -     //accessing a struct in a struct body +printf"%s like to eat%s\n", F.name,f.care.food); -printf"%s like to exercise%f hours\n", f.name,f.care.exercise_hours); + } A  at intMain () { -     structFish snappy={"Snappy","Piranha", the,4,{"Meat",7.5}}; - catalog (snappy); -     return 0; -}

Simpler to create structure variables

Naming structures by using typedef so that you can omit the struct keyword when you create a struct variable

#include <stdio.h>typedefstructcell_phone{intCell_no; Const Char*Wallpapaer;} Phone;//phone is a type name (alias for Cell_phone)intMain () {phone P={5555,"Sinatra.png"}; printf ("Number %i\n", P.cell_no); return 0;}

We can also omit the structure's name definition structure directly, which is the so-called anonymous structure

1 struct {2     int Cell_no; 3     Const Char *Wallpapaer; 4 } phone;
passing struct pointers

When assigning a struct to another structure, we know that a new copy is created, and if we want to update the original structure with the assigned structure, we need to use the struct pointer

1#include <stdio.h>2 3typedefstruct {4     intCell_no;5     Const Char*Wallpapaer;6 } phone;7 8 intMain () {9Phone p={5555,"Sinatra.png"};TenPhone p2=p; Onep2.cell_no=4444; Aprintf"p.cell_no:%i p2.cell_no:%i\n", p.cell_no,p2.cell_no); -phone* p3=&p;//assign the address of structure p to *p3 -(*P3). cell_no=6666; theprintf"p.cell_no:%i p2.cell_no:%i p3.cell_no:%i\n", P.cell_no,p2.cell_no, (*p3). cell_no); -     return 0; -}

Because we often put (*P2). Wallpapaer errors written *p2.wallpapaer, they are not equivalent, so C language developers have designed a simpler way to represent the structure of 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 }

C language Learning 014: structured data types

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.