二,结构
2.1 结构类型
2.1.1 声明结构类型:
函数内声明只能函数内使用,通常声明在函数外部,可以多个函数一起使用
#include<stdio.h>
int main() {
struct data {
int mouth;
int day;
int year;
};
//不要忘记分号
struct data today;
today.mouth = 07;
today.day = 31;
today.year = 2014;
printf("Todday's date is %i-%i-%i.\n",
today.day, today.mouth, today.year);
return 0;
}
#include<stdio.h>
struct data {
int mouth;
int day;
int year;
};
int main() {
struct data today;
today.mouth = 07;
today.day = 31;
today.year = 2014;
printf("Todday's date is %i-%i-%i.\n",
today.day, today.mouth, today.year);
return 0;
}
2.1.2 声明的形式
三种,结构类型,结构变量
struct data {
int mouth;
int day;
int year;
};
struct data today;
struct data jintian;
//有结构类型名字,有变量
struct {
int mouth;
int day;
int year;
}today,jintian;
//无名结构
struct data {
int mouth;
int day;
int year;
}today,jintian;
//声明结构,定义两个变量
2.1.3 结构初始化
#include<stdio.h>//结构初始化--近数组
struct data {
int mouth;
int day;
int year;
}today, jintian;
int main() {
//结构变量的使用一定要带上 STRUCT 结构名称 变量名
struct data today = { 07,31,2014 };
struct data jintian = { .mouth = 7,.year = 2014 };
printf("Todday's date is %i-%i-%i.\n",
today.day, today.mouth, today.year);
printf("Todday's date is %i-%i-%i.\n",
jintian.day, jintian.mouth, jintian.year);
return 0;
}
结构成员,数组用[ ]访问数组成员,结构用 . 运算符访问结构成员【访问单个成员】
要访问整个结构,直接用结构变量的名字
对于整个结构,可以赋值,取地址,也可以传递给函数参数
2.1.4 结构运算
#include<stdio.h>
struct data {
int mouth;
int day;
int year;
}today, jintian;
int main() {
struct data today = { 07,31,2014 };
struct data jintian = { .mouth = 3,.year = 2024 };
printf("Todday's date is %i-%i-%i.\n",
today.day, today.mouth, today.year);
printf("Todday's date is %i-%i-%i.\n",
jintian.day, jintian.mouth, jintian.year);
//赋值
today = jintian;
printf("Todday's date is %i-%i-%i.\n",
today.day, today.mouth, today.year);
struct data i;//INT I不行
i = (struct data){ 9,24,2024 };
//强制类型转换,但是I不可以INT 不懂
printf("Todday's date is %i-%i-%i.\n",
i.day, i.mouth, i.year);
i.year = 2014;
printf("Todday's date is %i-%i-%i.\n",
i.day, i.mouth, i.year);
return 0;
}
2.1.5 结构指针
结构变量的名字不是结构变量的地址,需要使用&运算符
#include<stdio.h>
struct data {
int mouth;
int day;
int year;
}today, jintian;
int main() {
struct data today = { 3,23,2024 };
struct data* pData = &today;
scanf_s("%d", pData);
printf("%i-%i-%i\n", today.mouth, today.day, today.year);
//只能输入MOUTH
return 0;
}
没有直接的办法可以一次 scanf 一个结构
2.2 结构与函数
2.2.1 结构作为函数参数
整个结构可以作为 参数的值 传入函数,可以在函数内新建一个结构变量,并复制调用者的结构的值,也可以返回一个结构
#include<stdio.h>//明天-日历
#include<stdbool.h>
struct date {
int mouth;
int day;
int year;
};
bool isLeap(struct date d);
//逻辑类型,TRUE=1,FAULTH=0
//判断闰年
int numofdays(struct date d);
//判断月份--天数
int main(int argc,char const* argv[]) {
struct date today, tom;
printf("Enter today's date (mm dd yyyy):");
scanf_s("%i %i %i", &today.mouth, &today.day, &today.year);
if (today.day != numofdays(today)) {
tom.day = today.day + 1;
tom.mouth = today.mouth;
tom.year = today.year;
}//不是当月最后一天
else if (today.mouth == 12) {
tom.day = 1;
tom.mouth = 1;
tom.year = today.year + 1;
}//是当月最后一天且是一年最后一天
else {
tom.day = 1;
tom.mouth = today.mouth + 1;
tom.year = today.year;
}
printf("TOMORROW'S DATE IS %i-%i-%i.\n",
tom.mouth, tom.day, tom.year);
return 0;
}
bool isLeap(struct date d) {
bool leap = false;
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0) {
leap = true;
}//非整百年份除4整除,整百除400,1900不是闰年
return leap;
}
int numofdays(struct date d) {
int days;
const int daysofmouth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (d.mouth == 2 && isLeap(d)) {
days = 29;
}
else {
days = daysofmouth[d.mouth - 1];
}
return days;
}
2.2.2 输入结构
没有直接的办法可以一次 scanf 一个结构
#include<stdio.h>
#include<stdbool.h>
struct point {
int x;
int y;//多打了一个分号直接去世
};
void get(struct point p) {
scanf_s("%d", &p.x);
scanf_s("%d", &p.y);
printf("%d,%d\n", p.x, p.y);
}
void put(struct point p) {
printf("%d,%d\n", p.x, p.y);
}
void main() {
struct point pp = { 0,0 };
get(pp);
put(pp);
return 0;
}
函数都是传值的,上面的函数操作的时候是设置了一个结构变量,复制了传进去的结构变量,但是没有改动他,出了函数,函数内的结构变量不起作用了
改动 1.0——返回结构变量
#include<stdio.h>
#include<stdbool.h>
struct point {
int x;
int y;
};
struct point get(void) {
struct point p;//不能忘记
scanf_s("%d", &p.x);
scanf_s("%d", &p.y);
printf("%d,%d\n", p.x, p.y);
return p;//不能忘记
}
void put(struct point p) {
printf("%d,%d\n", p.x, p.y);
}
int main() {
struct point pp = { 0,0 };
pp = get();
put(pp);
return 0;
}
2.2.3 指向结构的指针
struct data* pData = &today;
(*pData).mouth = 12;
p->mouth = 12;
改进 2.0 ,妈的嘎嘎出不来,烦,
#include<stdio.h>
struct cc{
int x;
int y;
};
struct cc* get(struct cc *p);
int main(int argc, const int* argv[]) {
//错误1111111啊啊啊啊啊啊
struct cc ddd = { 0,0 };
get(&ddd);
}
struct cc* get(struct cc* p) {
scanf_s("%d", p>x);
//错误2啊啊啊啊啊啊啊啊啊啊啊
scanf_s("%d", p>y);
printf("%d,%d\n", p->x, p->y);
}
所以是-》符号的问题吗?这玩意怎么一键打出来????啊
狗东西!!!int argc, char const* argv[ ] heheh
这个狗地方再错是小狗!!是!小!狗!、
CHAR CONST *ARGV[ ]是字符串数组,数组带入函数需要另一个整数告诉函数数组有多大,INT ARGC就是那个整数,两个变量,整数,字符串数组
#include<stdio.h>
struct cc{
int x;
int y;
};
struct cc* get(struct cc *p);
int main() {
struct cc ddd = { 0,0 };
get(&ddd);
return 0;
}
struct cc* get(struct cc* p) {
scanf_s("%d", &p->x);
scanf_s("%d", &p->y);
printf("%d,%d\n", &p->x,&p->y);
printf("%d,%d\n", p->x, p->y);
//妈的,妈的,妈的//是&p->x
return p;
}
我真的,睁大我的狗眼看清楚啊啊啊啊,P->X是一个变量,SCANF是对地址操作,所以还要取一次地址,啊啊啊啊啊我的脑子!!!谁吃了我的脑子!!!
啊啊啊啊啊啊啊啊啊啊啊啊!!!多~么~痛~的~零~五~~~
#include<stdio.h>
struct cc{
int x;
int y;
};
struct cc* get(struct cc *p);
void put(struct cc);
void put2(struct cc* p);
int main() {
struct cc ddd = { 0,0 };
get(&ddd);
put(ddd);
put(*get(&ddd));
put2(&ddd);
put2(get(&ddd));
*get(&ddd) = (struct cc){ 1,2 };//稍微有点不懂,为什么要强制类型转换
printf("%d %d", ddd.x, ddd.y);
//GET函数包括输入,所以,MAIN里面要输入4次
return 0;
}
struct cc* get(struct cc* p) {
scanf_s("%d", &p->x);
scanf_s("%d", &p->y);
printf("%d,%d\n", &p->x,&p->y);
printf("%d,%d\n", p->x, p->y);
//妈的,妈的,妈的//是&p->x
//传进来是指针P还是&ddd都一样啊啊啊啊
return p;
}
void put(struct cc p) {
printf("%d %d\n", p.x, p.y);
}
void put2(struct cc *p) {
printf("%d %d\n", p->x, p->y);
}