#include <stdio.h>
int main(int argc, char const *argv[])
{
// 声明结构类型
struct date
{
int month;
int day;
int year;
};
// 使用自定义的类型
struct date today;
today.month = 9;
today.day = 30;
today.year = 2019;
printf("Today is date is %i-%i-%i\n", today.year , today.month, today.day);
return 0;
}
#include <stdio.h>
struct date
{
int month;
int day;
int year;
};
int main(int argc, char const *argv[])
{
struct date today;
today.month = 9;
today.day = 30;
today.year = 2019;
printf("Today is date is %i-%i-%i\n", today.year , today.month, today.day);
return 0;
}
struct point{
int x;
int y;
}
struct point p1,p2;
// 其中p1和p2都是point
// 里面有x和y的值
还有另外一种形式:
struct{
int x;
int y
}p1 , p2 ;
// p1和p2都是一种无名结构,里面有x和y
当然了,还有一种更加常用的形式
struct point {
int x;
int y;
}p1 , p2;
p1和p2都是point,里面有x和y的值
struct date{
int month;
int day;
int year;
};
int main(int argc, char const *argv[])
{
struct date taday = {9 , 30 , 2019};
struct date thismonth = {.month=9 , .year=2019};
printf("Today is date is %i-%i-%i\n", taday.year,taday.month,taday.day );
printf("This month is %i-%i-%i\n", thismonth.year,thismonth.month,thismonth.day);
return 0;
}
// Today is date is 2019-9-30
// This month is 2019-9-0
int numberOfDays(struct date d);
#include <stdio.h>
struct point
{
int x;
int y;
};
void getStruct(struct point p);
void output(struct point p);
int main()
{
struct point y = {0,0};
getStruct(y);
output(y);
return 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);
}
// 2
// 3
// 2 , 3
// 0 , 0
#include <stdio.h>
struct point
{
int x;
int y;
};
struct point getStruct(void);
void output(struct point p);
int main()
{
struct point y = {0,0};
y = getStruct();
output(y);
return 0;
}
// 这里返回一个struct
struct point getStruct(void){
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);
}
// 5 6
// 5 , 6
// 5 , 6
struct date
{
int month;
int day;
int year;
}myday;
struct date *p = &myday;
(*p).month = 12;
p->month = 12;
用->表示指针所指的结构变量中的成员
struct point
{
int x;
int y;
};
struct point* getStruct(struct point *p);
void output(struct point p);
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));
return 0;
}
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);
}
void print(const struct point *p)
{
printf("%d , %d\n", p->x , p->y);
}
原文:https://www.cnblogs.com/mengd/p/11700158.html