C语言 结构体(超全)

#include<stdio.h>
#include<string.h>

/*
//结构体声明
struct book //struct 是关键词 、book是结构体类型名(可更换) 
{
    char title[100]; //题目 
    char author[100]; //作者 
    float value;  //价格 
}; //分号不能少 
int main()
{
    //结构体初始化 
    struct book s1 =
    {
        "yuwen","guojia",22.5
     };
     
     //访问结构体变量 
    printf("%s\n",s1.title);
    printf("%s\n",s1.author);
    printf("%f\n",s1.value);
    return 0;

*/ 


// struct book library; //把library设为一个可以使用book结构体的结构体变量,
                        //则library这个变量就包含了其book结构体中的所有元素


/*
//插入小知识点1
struct book 
{
    char title[100]; //题目 
    char author[100]; //作者 
    float value;  //价格 
};
int main()
{
    struct book s1;
    return 0;
}

等价于

struct book 
{
    char title[100]; //题目 
    char author[100]; //作者 
    float value;  //价格 
}s1;
*/

/*
//插入小知识点2
关于其struct声明的位置,也就是这段代码要放到哪里。同样这也是具有作用域的。
这种声明如果放在任何函数的外面,那么则可选标记可以在本文件中,该声明的后面的所有函数都可以使用。
如果这种声明在某个函数的内部,则它的标记只能在内部使用,并且在其声明之后
*/


/*
//标记名是可选的,那么什么时候可省,什么时候不可以省略呢? 
1.可省 : 设计的同时就创建该结构体变量,但这种设计是一次性的
  例子 : 
  struct 
  {
      char title[100];
      char author[100];
      float value;
  }library;
2.不可省:在一个地方定义结构体设计,且在其他地方定义实际的结构体变量,那么就必须使用标记 
*/

/*
typedef的用法: 
命名规则:只能使用字母、数字、下划线,且首字符不能是数字。 

格式: typedef 原类型名 新类型名

例子1: typedef  A  a       
例子2;//将 A类型名 用 s1类型名 替代 
    typedef struct A 
    {
    int a;
    int b;
    char name[100];
    }s1;
*/

/*
//结构体初始化
struct book s1=
    {
        "yuwen",//title为字符串 
        "guojiajiaoyun",//author为字符数组 
        22.5    //value为flaot型 
    };
    //要对应起来,用逗号分隔开来,与数组初始化一样;
//插入小知识3
如果定义结构体变量时没有初始化,那后面就不能全部一起初始化
只能单个进行赋值
需要用到 strcpy函数
例子

#include<stdio.h>
#include<string.h>
struct A
{
    char name[100];
};
int main()
{
    struct A a;
    strcpy(a.name,"hello");
    return 0;
}


*/

/*
//结构体指定初始化
struct book
{
    char title[100];
    char author[100];
    float value;
};

int main()
{
    struct book s1 =
    {
        .value = 11,
        .author = "guojia",
        .title = "yuwen",
        //"hello"
    };
    printf("%s\n%s\n%f\n",s1.title,s1.author,s1.value);
}
*/


/*
//访问结构体成员
格式 :结构体变量名.成员名; 
例子 : 
struct date
{
    int year;
    int month;
    int day;
}s1;
如果要调用date类型中的year  写s1.year
*/

/*
//整体赋值
例子
struct student
{
    char name[100];
    char author[100];
}s1;

struct book
{
    float value;
    int time;
}b1;

int main()
{
    struct student s2;
    s2 = s1;  //整体赋值 
    return 0;
}
//插入小知识点4 
重载运算符(c++的内容)
可以在不同类型间进行整体赋值 , 感兴趣的可以去查一查了解一下 
*/

/*
//结构体数组 
格式:类型名 数组名[] 
例子:struct book arr[100];     //解释:开了一个book类型的数组,数组名是arr 
//结构体数组初始化
#include<stdio.h>
struct book
{
    char name[100];
    char author[100];
    float value;
};
int main()
{
    struct book library[3] =
    {
        {"hao","guojia",22.2},
        {"hello","guojia",21.3},
        {"world","guojia",22.4}
    };
    for(int i = 0;i<3;i++)
    {
        printf("第%d个书信息:名字:%s 作者:%s 价格:%f\n",i+1,library[i].name,library[i].author,library[i].value); 
    }
    return 0;
}
*/

/*
//结构体数组的应用
问题:用结构体数组去输入一份学生名单 ,要包含名字,年龄,学号 (三个人就行) 

解答:
#include<stdio.h>
struct student
{
    char name[20];
    int age;
    int num;//学号 
};
int main()
{
    struct student s1[3];  //声明一个结构体数组
    for(int i = 0;i<3;i++)
    scanf("%s %d %d",s1[i].name,s1[i].age,s1[i].num);
    for(int i = 0;i<3;i++)
    printf("第%d个学生的信息:名字:%s 年龄:%d 学号:%d\n" ,i+1,s1[i].name,s1[i].age,s1[i].num);
    return 0;
}
*/ 

/*
//结构体嵌套
struct date
{
    int year;
    int month;
    int day;
};
struct student
{
    char name[100];
    struct date birthday;
}s1;

想引用student的出生年月日
可以表示为 s1.birthday.year 
格式:结构体变量名.成员.子成员 
*/

/*
//指针
*p、p以及&p的区别
p是指针的名字,表示指针指向的内存地址
*p表示此指针指向的内存地址所存放的内容
&p 表示指针自己的地址 
//结构体指针
声明结构体指针
格式:struct 结构体名 * 指针名;
例子:struct book * him; //意思是:创建了一个book类型的him指针变量,它可以指向任何现有的book类型的变量 

struct book
{
    char name[100];
    char author[100];
    float value;
};
int main()
{
    struct book library[3];
    struct book * him;
    him = library[0];  //指针指向结构体library[0] 
    return 0;
}
*/


/*
//结构体指针的应用 
1 引入一个运算符  ->     它和.运算符是一样的操作
    区别  -> 用于结构体指针访问成员
          .  用于结构体变量名访问成员
          

例子
#include<stdio.h>
struct grade
{
    char name[100];
    float max;//最高分 
    float min;//最低分 
};
int Average(grade * s)
{
    return (s->max + s->min)/2;
}
int main()
{
    struct grade g1 =
    {
        "zhangsan",100,50
    };
    int average = Average(&g1);
    printf("%d",average);
    return 0;
}
 

2 & 和 *是互逆运算符
例子:如果 him = & library[0]; 那么 *him = library[0]; 

所以 library[0].value  <=>  (*him).value 
*/


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值