C高级之结构体
用户自定义的数据类型,结构体中可以包含若干相同或不同数据类型的成员变量,使这些数据项组合起来反映某种信息。struct 结构体名成员变量;struct结构体名变量名;int id;int age;//全局//局部return 0;struct 结构体名成员变量;} 变量名;int id;int age;}stu;//全局//局部return 0;注意: 此种方式无法在局部再去单独定义变量,只能在全
C高级:结构体,结构体数组,结构体指针,共用体,枚举,存储类型,函数指针,指针函数,函数指针数组
Linux:软件安装、用户管理、进程管理、shell通用、硬链接和软链接、解压和压缩、功能性语句、结构性语句、脚本编程、份文件、make工具
一、定义
用户自定义的数据类型,结构体中可以包含若干相同或不同数据类型的成员变量,使这些数据项组合起来反映某种信息。
二、格式
struct 结构体名
{
数据类型 成员变量1;
数据类型 成员变量2;
数据类型 成员变量3;
。。。
};
struct 结构体名:用户自定义的数据类型的名字,和int 、char 等作用相同
三、结构体变量
1.概念
通过结构体类型定义的变量
2.格式
struct 结构体名 变量名 ;
2.1 先定义结构体,再定义结构体变量
struct 结构体名
{
成员变量;
};
struct 结构体名 变量名;
#include <stdio.h>
struct student
{
int id;
int age;
float score;
};
//全局
//struct student stu;
int main(int argc, char const *argv[])
{
//局部
struct student stu;
return 0;
}
2.2定义结构体同时去定义结构体变量
struct 结构体名
{
成员变量;
} 变量名;
#include <stdio.h>
struct student
{
int id;
int age;
float score;
}stu; //全局
int main(int argc, char const *argv[])
{
//struct student stu;//局部
return 0;
}
2.3缺省结构体名定义去定义结构体变量
注意: 此种方式无法在局部再去单独定义变量,只能在全局定义!!
struct
{
成员变量;
} 变量名;
#include <stdio.h>
struct
{
int id;
int age;
float score;
}stu;
int main(int argc, char const *argv[])
{
return 0;
}
3.赋值
3.1 定义变量的同时直接大括号赋值
必须按顺序
#include <stdio.h>
struct student
{
int id;
int age;
float score;
}stu;
int main(int argc, char const *argv[])
{
struct student stu={3,23,98.5};//id=3;age=23;score=98.5
return 0;
}
3.2定义变量时未初始化,需要对各个成员变量一对一赋值
使用成员运算符:.
#include <stdio.h>
struct student
{
int id;
int age;
float score;
}stu;
int main(int argc, char const *argv[])
{
stu.age=23;
stu.id=3;
return 0;
}
3.3 点等法赋值
使用大括号但不按顺序时
#include <stdio.h>
struct student
{
int id;
int age;
float score;
}stu;
int main(int argc, char const *argv[])
{
struct student stu={
.age=23,
.id=3,
.score=98.5
};
return 0;
}
4. 访问成员变量
通过 “ . ” 访问 => 结构体变量名 . 成员变量名
#include <stdio.h>
struct student
{
int id;
int age;
char name[10];
float score;
}stu;
int main(int argc, char const *argv[])
{
struct student stu;
scanf("%d %f %s",&stu.id,&stu.score,stu.name);
printf("%d %.2f %s",stu.id ,stu.score, stu.name);
putchar(10);
return 0;
}
需不需要 & 还是要看成员变量的数据类型

四、结构体数组
1.概念
结构体类型相同的变量组成的数组
2.格式
2.1 定义结构体的同时定义结构体数组
#include <stdio.h>
struct student
{
char name[10];
int id;
int class;
float score;
}stu[10];
int main(int argc, char const *argv[])
{
return 0;
}
2.2先定义结构体,然后再定义结构体数组
#include <stdio.h>
struct student
{
char name[10];
int id;
int class;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu[10];
return 0;
}
3.赋值
3.1定义结构体数组的同时赋值
#include <stdio.h>
struct student
{
char name[10];
int id;
int class;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu[10]={
{"amy",3,0,90},//第一个学生的信息
{"tom",4,0,98},//第二个学生的信息
};
return 0;
}
3.2先定义结构体数组,再对结构体数组每个元素进行赋值
#include <stdio.h>
struct student
{
char name[10];
int id;
int class;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu[10];
stu[0].id=3;
stu[1].class=1;
return 0;
}
4.结构体数组输入输出(使用for循环)
#include <stdio.h>
struct student
{
char name[10];
int id;
int class;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu[10];
for (int i = 0; i < 3; i++)
{
scanf("%d", &stu[i].id);
}
return 0;
}
5. 结构体数组大小
数组元素个数 ×sizeof(结构体类型名)
结构体大小不是简单的成员变量大小的叠加,要遵循边界对齐原则
五、结构体指针
1.概念
指向结构体的指针
2.定义格式
struct 结构体名 * 结构体指针变量名

3.访问
对于结构体变量的间接访问(通过指针):
(1)使用 * : 不常用
struct student *p1=&stu1; (*p1).id=2;. 的优先级要高于*,所以要加()
(2)使用 -> : 常用
格式:结构体指针变量名 -> 成员变量名
struct student *p1=&stu1; p1->id=3; scanf("%d",&p1->class);注意:成员运算符( . -> )的优先级高于单目运算符( * &)
通过结构体指针变量取成员变量的地址:
&结构体指针名->成员变量名
例:&p1->id;
例题:
#include <stdio.h> struct sale { int id; char name[10]; int price; } book[3]; void findBook(struct sale *p, int a); int main(int argc, char const *argv[]) { //先输入书籍信息 for (int i = 0; i < 3; i++) { scanf("%d %s %d", &book[i].id, book[i].name, &book[i].price); } printf("售价高于指定值的书籍有:\n"); findBook(book, 20); return 0; }对于findBook()函数:
第一种写法:(使用指针间接访问数组 ,即使用 p[i] )
第二种写法:(利用 p+i 表示 p[i] )
4.结构体指针大小
32位OS 4B
64位OS 8B
总结:
1. 不能把结构体类型变量作为整体引用,只能对结构体类型变量中的成员变量分别引用
2. 如果成员变量本身属于另一种结构体类型,用若干个成员运算符一级级找到你想要的成员变量
#include <stdio.h>
struct student
{
int id;
int class;
float score;
};
struct work
{
int num;
struct student stu;
}w1;
int main(int argc, char const *argv[])
{
w1.stu.id=10; //逐级查找然后一对一赋值
struct work w1={10,{1,2,98.5}};//直接赋值
return 0;
}
3. 可以把成员变量当成普通变量运算
4. 在数组中,数组之间不能彼此赋值,结构体变量可以相互赋值
int a[3]={1,2,3};
int b[3]={};
b=a; // 错误的!!!
struct student
{
int id;
int class;
char name[10];
}stu1,stu2;
int main(int argc, char const *argv[])
{
stu1.id=0;
stu1.class=1;
stu1.name="amy";
stu2=stu1; // 正确的
return 0;
}
关键字typedef
关键字typedef ---->用于给数据类型重命名
例:
1.
typedef int size4;---> size4等价于int
2.
typedef struct message
{
成员变量;
}msg;
msg等价于 struct message
| typedef | #define |
|
| 本质 | 关键字,编译器处理 | 预处理指令,文本替换 |
| 作用域 | 遵循变量作用域规则(块/文件/全局) | 全局(除非在函数内定义) |
| 类型安全 | 是(编译器检查类型一致性) | 否(纯文本替换,无类型检查) |
| 调试信息 |
显示别名(如 typedef其实就是对类型的一种重定义 |
显示原始类型(如 int) |
例:



更多推荐




所有评论(0)