结构类型
声明结构类型
#include<stdio.h>
int main()
{
struct date {
int month;
int day;
int year;
};
struct date today;
today.day =6;
today.month = 8;
today.year = 2022;
printf("%i年%i月%i日。\n", today.year, today.month, today. day);
return 0;
}
函数内部申明的结构类型只能在函数内部使用
声明结构变量的形式:
- p1和p2都是point里有x和y的值
struct point{
int x;
int y;
};
struct point p1,p2;
- p1和p2都是一种无名结构,里面有x和y`
struct{
int x;
int y;
}p1,p2;
- p1和p2都是point里有x和y的值
struct point{
int x;
int y;
}p1,p3;
第一和第三种都声明了结构point,第二种形式没有声明point,知识定义了两个变量。
结构的初始化
#include<stdio.h>
int main()
{
struct date {
int year;
int month;
int day;
};
struct date today = { 2022,8,06 };
struct date thismonth = { .year = 2022,.month = 8 };
printf("%i年%i月%i日。\n", today.year, today.month, today. day);
printf("%i年%i月%i日。\n",thismonth.year, thismonth.month,
thismonth.day);
return 0;
}
结构成员
- 结构类似与数组
- 数组用[ ]运算符和下标访问其成员
- 结构用 . 运算符和名字访问其成员
结构运算
-
要访问整个结构,直接用结构变量的名字;
-
对于整个结构,可以做赋值,取地址,也可以传递给函数参数
p1=(struct point){5,10};//相当于p1.x=5;p1.y=10; -
p1=p2;//相当于p1.x=p2.x;p1.y=p2.y;
#include<stdio.h>
int main()
{
struct date {
int year;
int month;
int day;
};
struct date today ;
today = (struct date){ 2022,8,06 };
struct date day;
day = today;
printf("%i年%i月%i日。\n", today.year, today.month, today. day);
printf("%i年%i月%i日。\n",day.year, day.month, day.day);
return 0;
}
结构指针
- 结构变量的名字并不是结构变量的地址,必须使用&运算符
- struct date *point=&today;
结构与函数
结构作为函数参数
int numberOfDays(struct date d)
- 整个结构可以作为参数的值传入函数
- 这个时候是在函数内新建一个结构变量,并复制调用者的结构的值
- 也可以返回一个结构
#include<stdio.h>
#include<stdbool.h>
struct date {
int month;
int day;
int year;
};
bool isLeap(struct date d);
int numberOfDays(struct date d);
int main()
{
struct date today, tomorrow;
printf("Enter today's date(mm dd yyyy):");
scanf("%i %i %i", &today.month, &today.day, &today.year);
if (today.day != numberOfDays(today)) {
tomorrow.day = today.day + 1;
tomorrow.month = today.month;
tomorrow.year = today.year;
}
else if (today.month == 12) {
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year+1;
}
else {
tomorrow.day = 1;
tomorrow.month = today.month+1;
tomorrow.year = today.year;
}
printf("Tomorrow's date is %i-%i-%i.\n", tomorrow.year, tomorrow.month, tomorrow.day);
return 0;
}
int numberOfDays(struct date d)
{
int days;
const int daysPerMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,12 };
if (d.month == 2 && isLeap(d))
days = 29;
else
days = daysPerMonth[d.month - 1];
return days;
}
bool isLeap(struct date d)
{
bool leap = false;
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
leap = true;
return leap;
}
输入结构
写一个函数来读入和输出结构
#include<stdio.h>
struct point {
int x;
int y;
};
struct point getStruct(void);
void output(struct point);
int main(int argc, char const* argv[])
{
struct point y = { 0,0 };
getStruct(y);
y=getStruct();
output(y);
}
struct point getStruct(struct point p)
{
struct point p;
scanf("%d", &p.x);
scanf("%d", &p.y);
printf("%d,%d\n", p.x, p.y);
return p;
}
void output(struct point p)
{
printf("%d,%d", p.x, p.y);
}
C语言传递的是值,因此要送回读入的值只能通过赋值,输出为0,0
指向结构的指针
#include<stdio.h>
struct point {
int x;
int y;
};
struct point* getStruct(struct point* p);
void output(struct point);
void print(const struct point* p);
int main(int argc, char const* argv[])
{
struct point y = { 0,0 };
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
}
struct point* getStruct(struct point* p)
{
scanf("%d", &p->x);
scanf("%d", &p->y);
printf("%d,%d\n", p->x, p->y);
return p;
}
void output(struct point p)
{
printf("%d,%d", p.x, p.y);
}
void print(const struct point* p)
{
printf("%d,%d", p->x, p->y);
}
用->表示指针所指的结构变量中的成员
结构中的结构
结构数组
结构数组初始化:
struct time testTimes[5] = {
{11,59,59},{12,0,0},{1,29,59},{23,59,59},{19,12,27}
};
#include<stdio.h>
struct time {
int hour;
int minutes;
int seconds;
};
struct time timeUpdate(struct time now);
int main(void)
{
struct time testTimes[5] = {
{11,59,59},{12,0,0},{1,29,59},{23,59,59},{19,12,27}
};
int i;
for (i = 0;i < 5;++i) {
printf("Time is %.2i:%.2i:%.2i\n",
testTimes[i].hour, testTimes[i].minutes, testTimes[i].seconds);
testTimes[i] = timeUpdate(testTimes[i]);
printf("One seconds later is: %.2i:%.2i:%.2i\n",
testTimes[i].hour, testTimes[i].minutes, testTimes[i].seconds);
}
return 0;
}
struct time timeUpdate(struct time now)
{
++now.seconds;
if (now.seconds == 60) {
now.seconds = 0;
++now.minutes;
if (now.minutes == 60) {
now.minutes = 0;
++now.hour;
if (now.hour == 24) {
now.hour = 0;
}
}
}
return now;
}
结构中的结构
结构中的结构的数组
类型定义
自定义数据类型(typedef)
- C语言提供了一个叫做typedef的功能来声明一个已有的数据类型的新名字。比如:
typedef int Length;
使得Length成为int类型的别名 - 这样,Length这个名字就可以代替int出现在变量定义和参数声明的地方了
Length a,b,len;
Length number[]; - Typedef声明新的类型的名字:
-
- 新的名字是某种类型的别名
-
- 改善了程序的可读性
typedef long int64_t; //重载已有的类型名字,新名字的含义更清晰;具有可移植性
typedef struct ADate{
int month;
int day;
int tear;
}Date;
int64_t i=1000000000000;
Date d={9,1,2005};