c语言【结构类型】- 结构体
声明结构的形式:
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
int main() {
struct date today;
today=(struct date){01,31,2014};
struct date day;
day=today;
day.year=2015;
printf("today=%d-%d-%d\n",today.year,today.month,today.day);//2014-1/31
printf("day=%d-%d-%d\n",day.year,day.month,day.day);//2015-1-31
return 0;
}
数组变量的名字就是数组的地址,而结构变量的名字并不是结构变量的地址,必须使用&运算符。
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
int main() {
struct date today;
//today=(struct date){01,31,2014};
struct date *pDate=&today;
printf("%p\n",pDate);//000000000062FE30
printf("%p\n",&today);//000000000062FE30
return 0;
}
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
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;
}
int numberOfDays(struct date d){
int days;
const int daysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(d.month==2&&isLeap(d)){
days=29;
}else{
days=daysPerMonth[d.month-1];
}
return days;
}
int main() {
struct date today,tom;
scanf("%d%d%d",&today.month,&today.day,&today.year);
if(today.day!=numberOfDays(today)){
tom.day=today.day+1;
tom.month=today.month;
tom.year=today.year;
}else if(today.month==12){
tom.day=1;
tom.month=1;
tom.year=today.year+1;
}else{//在某月最后一天
tom.day=1;
tom.month=today.month+1;
tom.year=today.year;
}
printf("tomorrow's date is '%d-%d-%d'\n",tom.year,tom.month,tom.day);
return 0;
}
输⼊结构
#include <stdio.h>
struct point {
int x;
int y;
};
void getStruct(struct point);
void output(struct point);
int main() {
struct point y = { 0, 0 };
getStruct(y);//12 23
output(y);//0 0
}
void getStruct(struct point p) {
scanf("%d", &p.x);
scanf("%d", &p.y);
printf("%d, %d\n", p.x, p.y);
}
void output(struct point p) {
printf("%d, %d\n", p.x, p.y);
}
#include <stdio.h>
struct point {
int x;
int y;
};
struct point getStruct();
void output(struct point);
int main() {
struct point y = { 0, 0 };
y=getStruct();//12, 23
output(y);//12, 23
}
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\n", p.x, p.y);
}
指向结构的指针
一般情况下我们传递结构体变量都是传地址以减少赋值内存内容的开销
#include <stdio.h>
struct point {
int x;
int y;
};
struct point* getStruct(struct point *p){
scanf("%d%d",&p->x,&p->y);
printf("%d %d\n",p->x,p->y);
return p;
}
void output(struct point p) {
printf("%d %d\n", p.x, p.y);
}
void print(const struct point *p){
printf("%d %d\n",p->x,p->y);
}
int main() {
struct point y = { 0, 0 };
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
*getStruct(&y)=(struct point){1,2};
}
结构数组
结构中的结构
结构中的结构的数组
#include <stdio.h>
struct point {
int x;
int y;
};
struct rectangle {
struct point p1;
struct point p2;
};
void printRect(struct rectangle r)
{
printf("<%d, %d> to <%d, %d>\n", r.p1.x, r.p1.y, r.p2.x, r.p2.y);
}
int main()
{
int i;
struct rectangle rects[] = {
{ { 1, 2 },{ 3, 4 } },
{ { 5, 6 },{ 7, 8 } }
}; // 2 rectangles
for (i = 0; i < 2; i++) {
printRect(rects[i]);
}
return 0;
}
/*
<1, 2> to <3, 4>
<5, 6> to <7, 8>
*/
typedef