结构体详注

 

 

/*

 数组:只能由多个相同类型的数据构成

 

 结构体:可以由多个不同类型的数据构成

 */

#include <stdio.h>

 

int main()

{

    //int ages[3] = {[2] = 10, 11, 27};

    

    

    //int ages[3] = {10, 11, 29};

    

    // 1.定义结构体类型

    struct Person

    { // 里面的3个变量,可以称为是结构体的成员或者属性

        int age; // 年龄

        double height; // 身高

        char *name; // 姓名

    };

    

    // 2.根据结构体类型,定义结构体变量

    struct Person p = {20, 1.55, "jack"};

    p.age = 30;

    p.name = "rose";

    

    printf("age=%d, name=%s, height=%f\n", p.age, p.name, p.height);

    

    /* 错误写法

    struct Person p2;

    p2 = {30, 1.67, "jake"};

    */

    

    struct Person p2 = {.height = 1.78, .name="jim", .age=30};

    //p2.age = 25;

    

    return 0;

}

 

//补齐算法

void test1()

{

    struct Student

    {

        int age;// 4个字节

        

        char a;

        

        //char *name; // 8个字节

    };

    

    struct Student stu;

    //stu.age = 20;

    //stu.name = "jack";

    // 补齐算法(对齐算法)

    // 结构体所占用的存储空间 必须是 最大成员字节数的倍数

    

    int s = sizeof(stu);

    printf("%d\n", s);

}

 

// 结构体内存细节

void test()

{

    // 1.定义结构体类型(并不会分配存储空间)

    struct Date

    {

        int year;

        int month;

        int day;

    };

    

    // 2.定义结构体变量(真正分配存储空间)

    struct Date d1 = {2011, 4, 10};

    

    

    struct Date d2 = {2012, 8, 9};

    

    // 会将d1所有成员的值对应地赋值给d2的所有成员

    d2 = d1;

    d2.year = 2010;

    

    printf("%d - %d - %d\n", d1.year, d1.month, d1.day);

    

    printf("%d - %d - %d\n", d2.year, d2.month, d2.day);

    /*

     printf("%p - %p - %p\n", &d1.year, &d1.month, &d1.day);

     

     int s = sizeof(d1);

     printf("%d\n", s);

     

     */

}

结构体例1

 

 

 

#include <stdio.h>

 

 

 

/*

 

 1.定义结构体变量的3种方式

 

 1> 先定义类型,再定义变量(分开定义)

 

 struct Student

 

 {

 

    int age;

 

 };

 

 struct Student stu;

 

 

 

 2> 定义类型的同时定义变量

 

 struct Student

 

 {

 

    int age;

 

 } stu;

 

 struct Student stu2;

 

 

 

 3> 定义类型的同时定义变量(省略了类型名称)

 

 struct

 

 {

 

    int age;

 

 } stu;

 

 

 

 2.结构体类型的作用域

 

 1> 定义在函数外面:全局有效(从定义类型的那行开始,一直到文件结尾)

 

 2> 定义在函数(代码块)内部:局部有效(从定义类型的那行开始,一直到代码块结束)

 

 */

 

 

 

// 从这行开始,一直到文件结尾,都是有效(跟全局变量一样)

 

struct Date

 

{

 

    int year;

 

    int month;

 

    int day;

 

};

 

 

 

int a;

 

 

 

void test2()

 

{

 

    struct Date

 

    {

 

        int year;

 

    };

 

    // 这里使用的是test2函数内部的struct Date类型

 

    struct Date d1 = {2011};

 

    

 

    

 

    // 结构体类型也是有作用域,从定义类型的那一行开始,一直到代码块结束

 

    struct Person

 

    {

 

        int age;

 

    };

 

    

 

    struct Person p;

 

    

 

    a  = 10;

 

}

 

 

 

int main()

 

{

 

    struct Date d1 = {2009, 8, 9};

 

    

 

    

 

    test2();

 

    

 

    // 不能使用test2函数中定义的类型

 

    // struct Person p2;

 

    

 

    return 0;

 

}

 

 

 

// 定义结构体变量

 

void test()

 

{

 

    // 定义结构体变量的第3种方式

 

    struct {

 

        int age;

 

        char *name;

 

    } stu;

 

    

 

    struct {

 

        int age;

 

        char *name;

 

    } stu2;

 

    

 

    

 

    /*结构体类型不能重复定义

 

     struct Student

 

     {

 

     int age;

 

     };

 

     

 

     struct Student

 

     {

 

     double height;

 

     };

 

     

 

     struct Student stu;

 

     */

 

    

 

    /* 错误写法:结构体类型重复定义

 

     struct Student

 

     {

 

     int age;

 

     double height;

 

     char *name;

 

     } stu;

 

     

 

     struct Student

 

     {

 

     int age;

 

     double height;

 

     char *name;

 

     } stu2;c

 

     */

 

    

 

    /*

 

     这句代码做了两件事情

 

     1.定义结构体类型

 

     2.利用新定义好的类型来定义结构体变量

 

     */

 

    // 定义变量的第2种方式:定义类型的同时定义变量

 

    /*

 

     struct Student

 

     {

 

     int age;

 

     double height;

 

     char *name;

 

     } stu;

 

     

 

     struct Student stu2;

 

     */

 

    

 

    /*

 

     // 定义变量的第1种方式:

 

     // 1.类型

 

     struct Student

 

     {

 

     int age;

 

     double height;

 

     char *name;

 

     };

 

       // 2.变量

 

     struct Student stu = {20, 1.78, "jack"};

 

     */

 

}

 

 

结构体例2

 

 

 

int main()

 

{

 

    struct RankRecord

 

    {

 

        int no; // 序号  4

 

        int score; // 积分 4

 

        char *name; // 名称 8

 

    };

 

    /*

 

    struct RankRecord r1 = {1, "jack", 5000};

 

    struct RankRecord r2 = {2, "jim", 500};

 

    struct RankRecord r3 = {3, "jake",300};

 

    */

 

    

 

    //int ages[3] = {10, 19, 29};

 

    

 

    //int ages[3];

 

    // 对齐算法

 

    // 能存放3个结构体变量,每个结构体变量占16个字节

 

    // 72

 

    /*

 

     int no; // 序号  4

 

     char *name; // 名称 8

 

     int score; // 积分 4

 

     */

 

    // 48

 

    /*

 

     int no; // 序号  4

 

     int score; // 积分 4

 

     char *name; // 名称 8

 

     */

 

    struct RankRecord records[3] =

 

    {

 

        {1, "jack", 5000},

 

        

 

        {2, "jim", 500},

 

        

 

        {3, "jake",300}

 

    };

 

     records[0].no = 4;

 

    // 错误写法

 

    //records[0] = {4, "rose", 9000};

 

     for (int i = 0; i<3; i++)

 

    {

 

        printf("%d\t%s\t%d\n", records[i].no, records[i].name, records[i].score);

 

    }

 

    //printf("%d\n", sizeof(records));

 

   return 0;

 

}

 

posted @ 2016-03-28 10:02  lance.xiang  阅读(115)  评论(0)    收藏  举报