C Primer Plus Reading notes the 14th chapter

Source: Internet
Author: User

This chapter mainly introduces the structure of C language and other data forms, is the focus of learning algorithm and data structure.

1. Sample Code

/*BOOK.C-a book catalogue containing only one books*/#include<stdio.h>#defineMAXTITL 41#defineMaxautl 31structbook{/*Data*/    CharTITLE[MAXTITL]; CharAuthor[maxautl]; floatvalue;};intMainvoid) {    structBook Library;/*declare the library as a variable of type book*/printf ("Please enter the book title.\n");    Gets (Library.title); printf ("Now Enter the author.\n");    Gets (Library.author); printf ("Now Enter the value.\n"); scanf ("%f", &library.value); printf ("%s by%s: $%.2f\n", Library.title, Library.author, Library.value); printf ("%s: \ "%s\" ($%.2f) \ n", Library.author, Library.title, Library.value); printf ("done.\n"); return 0;}

This example code shows how to build a struct declaration: Instead of creating an actual data object, it describes the elements that make up such objects (like templates in C + +). In fact, the structure declaration of book creates a new type named struct book. Other words

struct Book library;

Equivalent to

struct book{    Char TITLE[MAXTITL];     Char Author[maxautl];     float value;} Library;

You can also use this template only once, without having to tag book. The structure array is represented as follows.

struct book Library[maxbks];

2. Pointers to structures

Why use pointers to structures: 1. Pointers to structures are usually easier to manipulate than the structure itself; 2. In some early C implementations, structs cannot be passed as parameters to functions, but pointers to structures can; 3. Many wonderful data representations use structures that contain pointers to other structures.

/*FRIEND.C--using pointers to structures*/#include<stdio.h>#defineLEN 20structname{/*Data*/    CharFirst[len]; CharLast[len];};structguy{/*Data*/    structname handle; CharFavfood[len]; CharJob[len]; floatincome;};intMain (void) {    structGuy fellow[2] =     {        {{"Ewen","Villard"},        "Grilled Salmon",        "Personality Coach",        58112.0        },        {{"Rodney","swillbelly"},        "Tripe",        "Tabloid editor",        232400.0        }    }; structGuy * him;/*This is a pointer to the structure*/printf ("address #1:%p #2:%p\n", &fellow[0], &fellow[1]); Him= &fellow[0]; printf ("pointer #1:%p #2:%p\n", him, him +1); printf ("Him->income is $%.2f: (*him). Income is $%.2f\n", Him->income, (*him). Income); Him++; printf ("Him->favfood is%s:him->handle.last is%s\n", Him->favfood, him->handle.last); return 0;}

Understand the above code, basically know how to use the structure pointer. It is important to note that the name of a struct is not the address of the structure, which is not the same as the array and must use the & operator. The other is that the struct pointer followed by the operator is the same as the structure name of the followed. (point).

So, do we choose structure or structure pointers? Both have their advantages and disadvantages. Working with pointers as arguments can work early in C as well as in newer C, and execute quickly, with only a single address being passed at a time. The disadvantage is the lack of protection of the data. But we can use the Const qualifier to solve this problem. The programming style is also clearer when the structure is passed as parameter, and the original data is processed directly.

 

3. Other three types of data processing features

Here's a brief look at the other three characteristics of processing data: Union, enumeration, and typedef.

A union is a data type that can store different types of data in the same storage space (but at different time). Each variable is "mutually exclusive"-the disadvantage is not enough "containment", but the advantage is that memory usage is more granular and flexible, but also saves memory space.

An enumerated type is a collection of elements (enumeration members) that are named integral constants, separated by commas. is essentially an int type.

A typedef is the declaration of a new type name in place of the original type name.

C Primer Plus Reading notes the 14th chapter

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.