黑马程序员—结构体

来源:互联网 发布:聚宝网络 831226 编辑:程序博客网 时间:2024/05/30 22:46

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一、结构体

1、定义

     结构体是一个或多个变量的集合,这些变量可能为不同的类型,为了处理的方便而将这些变量组织在一个名字之下。

     定义结构体类型

struct Person

{

       int age;

       double height;

       char *name;

};//里面的三个变量,可以称为i结构体的成员或者属性


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

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

p.age = 30;

p.name = "rose";


      注意以下错误写法

struct Person p2;

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

//结构体与数组相同,初始化时直接赋值,结构体名代表地址不可以随便修改。


2、内存分析

     补齐算法(对齐算法):结构体所占用的存储空间,必须是最大成员字节数的倍数。

#include <stdio.h>int main(){    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);        return 0;}// 结构体内存细节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);          */}

3、注意点

     先定义类型,在定义变量(分开定义)

 struct Student

{

     int  age;

};

struct Student stu;

     定义类型的同时定义变量

struct Student

{

     int age;

}stu;

struct Student stu2;

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

struct

{

      int age;

}stu;

 

       结构体类型的作用域

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

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;}

三、指向结构体的指针

1、指向结构体的指针定义:struct Student *p;

2、利用指针访问结构体的成员:(*p).成员名称   p->成员名称

#include <stdio.h>int main(){    struct Student    {        int no;        int age;    };    // 结构体变量    struct Student stu = {1, 20};        // 指针变量p将来指向struct Student类型的数据    struct Student *p;        // 指针变量p指向了stu变量    p = &stu;        p->age = 30;        // 第一种方式    printf("age=%d, no=%d\n", stu.age, stu.no);        // 第二种方式    printf("age=%d, no=%d\n", (*p).age, (*p).no);        // 第三种方式    printf("age=%d, no=%d\n", p->age, p->no);        return 0;}


四、结构体和函数

       如果结构体作为函数参数,只是将实参结构体所有成员的值对应地赋值给了形参结构体的所有成员

       修改函数内部结构体的成员不会影响外面的实参结构体

#include <stdio.h>struct Student{    int age;    int no;};// 修改函数内部结构体的成员不会影响外面的实参结构体void test(struct Student s){    s.age = 30;    s.no = 2;}// 会影响外面的实参结构体void test2(struct Student *p){    p->age = 15;    p->no = 2;}void test3(struct Student *p){    struct Student stu2 = {15, 2};    p = &stu2;    p->age = 16;    p->no = 3;}int main(){    struct Student stu = {28, 1};        //test(stu);    //test2(&stu);    test3(&stu);        printf("age=%d, no=%d\n", stu.age, stu.no);        return 0;}

五、结构体的嵌套定义

#include <stdio.h>int main(){    struct Date    {        int year;        int month;        int day;    };            // 类型    struct Student    {        int no; // 学号                struct Date birthday; // 生日                struct Date ruxueDate; // 入学日期                // 这种写法是错误的        //struct Student stu;    };            struct Student stu = {1, {2000, 9, 10}, {2012, 9, 10}};        printf("year=%d,month=%d,day=%d\n", stu.birthday.year, stu.birthday.month, stu.birthday.day);        return 0;}




------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

0 0
原创粉丝点击