note_practical_c_programming_chapter12

Practical C Programming                             chapter 12 advance types

C provides a rich set of data types, including structures, unions, and enumerated types.

1. Structures

In a strcuture, each element or field is named and has its own data type.
The general form of a structure definition is
    struct structure-name {
        field-type field-name;    /* comment */
        field-type field-name;    /* comment */
        ...
    } variable-name;

The general form of a structure variable is
    struct structure-name variable-name;

To access field in a structure, use "." operator:
    variable.field;

Structures may be initialized at declaration time by putting the list of elements in curly braces ({}):
    /*
     * Printer cables
     */
    struct bin {
        char name[30];    /* name of the part */
        int quantity;    /* how many are in the bin */
        int cost;    /* The cost of a single part (in cents) */
    } print_cable_bin = {
        "Printer Cables",    /* Name of the item in the bin */
        0,    /* Start with empty box */
        1295    /* cost -- $12.95 */
    };

2. Unios
A union is similar to a structure; however, it defines a single location that can be given many different field name:
    union value {
        long int i_value;    /* integer version of value */
        float f_fvalue;        /* floating version of value */
    };

the field i_value and f_value share the same space;
In a union, all fields occupy the same space, so only one may be active at a time. In other words, if you put someting in i_value, assiging something to f_value wipes out the old value of i_value.

Unions are frequently used in the area of communications. For example, suppose we have a remote tape and we want to send it four messages: open, close, read, and write.
#define DATA_MAX 1024    /* Maximum amount of data for a read and write */

struct open_msg {
    char name[30];    /* Name of tape */
};

struct read_msg {
    int length;    /* Max data to transfer in the read */
};

struct write_msg {
    int length;    /* Number of bytes to write */
    char data[DATA_MAX];    /* Data to write */
};

struct close_msg {
    
};

const int OPEN_CODE=0;    /* Code indicating an open message */
const int READ_CODE=1;    /* Code indicating a read message */
const int WRITE_CODE=2;    /* Code indicating a write message */
const int CLOSE_CODE=3;    /* Code indicating a close message */

struct msg {
    int msg;    /* Message type */
    union {
        struct open_msg open_data;
        struct read_msg read_data;
        struct write_msg write_data;
        struct close_msg close_data;
    } msg_data;
};

3. typedef
C allows the programmer to define her own variable types through the typedef statement. This statement provides a way for the program to extend C's basic types. The general form of the typedef statement is:
    tyepdef type-declaration;
where type-declaration is the same as a variable declaration except that a type name is used instead of a variable-name.
One frequent use of typedef's is in the definition of a new structure:
    struct complex_struct {
        double real;
        double imag;
    };
    typedef struct complex_struct complex;
the same as:
    typedef struct complex_struct {
        double real;
        double imag;
    } complex;

where the complex_struct always is omitted.

4. enum Type
The enumerated data type is designed for variables that contain only a limited set of values. These values are referenced by name(tag). The complier assigns each tag an integer value internally.
The general form of an enmu statement is:
    enum enum-name { tag-1, tag-2, ... } variable-name;
C implements the enum types as compatible with integer.

5. Casting
this is accomplished through the cast or typecast operation. the general form of a cast is:
    (type) expression
This operation tells C to compute the value of the expression, and then convert it to the specified type.

6. Bit Fields or Packed Structures
Packed structures allow us to declare structures in a way that takes up a minimum amount of storage. for example:
    struct item {
        unsigned int list:1;    /* true if item is in the list */
        unsigned int seen:1;    /* true if this item has been seen */
        unsigned int number:14;    /* item number */
    };

Packed structures should be used with care. The code to extract data from bit fields is relatively large and slow. Unless storage is a problem, packed structures should not be used.

Using packed structures for flags is clearer and less error than using bitwise operator. However, bitwise operators given the programmer additional flexibility.

7. Arrays of Structures
Structures and arrays can be combined.
Initialization of an array of structures is similar to the initialization of multi-dimensional arrays.

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值