黑马程序员——结构体
来源:互联网 发布:变魔术的软件 编辑:程序博客网 时间:2024/05/15 02:37
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
全局变量和局部变量:
1、 局部变量:在函数(代码块)内部定义的变量;
作用域:从定义那一行开始,一直到代码块结束;
生命周期:从定义的那一行分配存储空间,代码块结束后就会被回收。
没有固定的初始值
2、 全局变量:在函数外定义的变量
作用域:从定义那一行开始,一直到文件结尾(能被后面的所有函数共享)
生命周期:程序一启动就分配空间,程序退出才被销毁。
默认的初始值为0
结构体 构造类型
定义结构体:
<span style="font-size:18px;">struct person{int age;double height;char *name;};</span>
根据结构体类型,定义结构体变量:
里面的成为三个成员
struct person p = {20, 1.55, “jack”}; p.age = 30;//更改年龄
struct person p1 = {30, 1.75, “jim”};//只能在定义结构体变量的时候赋值。
以上不能写为struct person p1;p1= {30, 1.75, “jim”};
结构体内存分析:
<span style="font-size:18px;">#include <stdio.h>int main(){// 定义结构体类型,定义类型的时候没有分配存储空间,就行int 一样。struct date{ int year; int month; int day;};//定义结构体变量struct date d1 = {2009, 4 , 10};struct date d2 = {2012, 6, 12};//将d1所有成员的值对应的赋值给d2的所有成员d2 = d1;printf("%d-%d-%d\n", d1.year, d1.month, d1.day);printf("%d-%d-%d\n", d2.year, d2.month, d2.day); //两个输出的结果都是2009-4-10return 0;}</span><span style="font-size:14px;"></span>
结构体内存细节:
#include <stdio.h>int main(){struct student{int age;//4个字节char *name;//8个字节};struct student stu;stu.age = 20;stu.name = "jack";int s = sizeof(stu);printf("%d\n", s); /*实际输出结果是16 而不是12。因为结构体分配存储空间默认是最大成员字节数的倍数。成为补齐算法(对齐算法)*/return 0;}
结构体注意点:
1、定义结构体的第二种方法:struct student{ int age; double height; char *name;} stu; //利用新定义好的类型来定义结构体变量,就像 int a; 一样
2、定义结构体变量的第三种方式,但这种方式不能重用,既不能写struct stu2;
struct { int age; double height; char *name;} stu;
结构体类型的作用域:从定义类型的那一行开始到代码块结束。例如一个函数中的结构类型只能用在这个函数中。
要想通用,应该定义全局结构体变量。
结构体数组
例如一个游戏的排行榜。#include <stdio.h>int main(){ struct rank{int num; // 序号char *name; // 名称int score; // 积分};struct rank records[3] = /*要想改写里面的值不能用records[0] = {4, “hanmei”, 100}; 可写为records[0].num = 4; */{ {1, "jack", 500}, {2, "rose", 300}, {3, "jim", 200}};for(int i = 0; i<3; i++){printf("%d\t%s\t%d\n", records[i].num, records[i].name, records[i].score );}return 0;}
指向结构体的指针:
#include <stdio.h>int main(){ struct student{int num; int age;};struct student stu = {1, 25};// 指针变量p指向struct student类型的数据struct student *p;p = &stu;// 第一种输出方法ptintf("num = %d, age = %d\n", stu.num, stu.age );// 第二种方法ptintf("num = %d, age = %d\n", (*p).num, (*p).age );// 第三种方法ptintf("num = %d, age = %d\n", p->num, p->age ); // 也可以用p->num来修改值return 0;}
结构体的嵌套
<span style="font-size:18px;">#include <stdio.h>int main(){ struct date{int year;int month;int day;};struct student{int num; //学号struct date birthday;//生日struct date ruxuedate;//入学日期};struct student stu = {1, {2009, 8, 25},{2012, 6, 1}};printf("%d\n", stu.birthday.year);//调用输出出生的年return 0; </span>
<span style="font-size:18px;">}</span>
0 0
- 黑马程序员—结构体
- 黑马程序员——结构体
- 黑马程序员——结构体
- 黑马程序员——结构体
- 黑马程序员——结构体
- 黑马程序员—C重点--结构体
- 黑马程序员—内存结构
- 黑马程序员-结构体
- 黑马程序员————结构体
- 黑马程序员————C语言结构体
- 黑马程序员————结构体学习笔记
- 黑马程序员——C基础——结构体
- 黑马程序员———C语言 结构体
- 黑马程序员——foundation框架之结构体
- 黑马程序员—C语言—结构体
- 黑马程序员——C语言结构体
- 黑马程序员——Foundation结构体和常用类
- 黑马程序员——C语言------结构体、枚举
- 年轻工程师为何只能卖个“白菜价”?
- 【比特币】脑钱包
- Hibernate查询之list/iterator
- MYSQL数据库引擎分析
- 字符串匹配算法总结
- 黑马程序员——结构体
- Visual Studio 2013开发 mini-filter driver step by step 应用层与内核通讯(8)
- mysql之union联合查询、子查询、连接查询
- 一小段代码演示C#接口的类级别实现和显式接口成员实现
- 程序语言基础知识总结
- oracle如何保证事务的ACID原则--oracle核心技术读书笔记二
- 重载重写与重定义
- C++对象在内存中的布局(读汇编代码)
- ARM接口技术——基础