结构作为函数参数
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(int argc, char const *argv[])
{
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,31};
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;
}
Enter today's date (mm dd yyyy):02 28 2020
Tomorrow's date is 2020-3-1.
输入结构
- 没有直接的方式可以一次scanf一个结构
- 如果我们打算写一个函数来读入结构
- 但是读入的结构如何送回来呢?
- 记住C在函数调用是是传值的
- 所以函数的p与main中的y是不同的
- 在函数读入了p的熟知之后,没有任何东西回到main,所以y还是{0,0}
#include<stdio.h>
struct point {
int x;
int y;
};
void getStruct(struct point);
void output(struct point);
void main(){
struct point y = {0,0};
getStruct(y);
output(y);
}
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",p.x, p.y);
}
2
2
2,2
0,0
解决的方案
-
之前的方案,把一个结构传入了函数,然后在函数中操作,但是没有返回回去
- 为题在于传入函数的是外面那个结构的克隆体,而不是指针
- 传入的结构和传入数组是不同的
- 为题在于传入函数的是外面那个结构的克隆体,而不是指针
-
在这个输入函数中,完全可以创建一个临时的结构变量,然后把这个结构返回给调用者
#include<stdio.h>
struct point {
int x;
int y;
};
struct point getStruct(void);
void output(struct point);
int main(int argc, char const *args[])
{
struct point y = {0,0};
y = getStruct();
output(y);
}
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", p.x, p.y);
}
2 3
2,3
2,3
指向结构的指针
struct date {
int month;
int day;
int year;
} myday;
struct date *p = &myday;
(*p).month = 12;
p->month = 12;
- 用->表示指针所指的结构变量中的成员
结构指针参数
void main()
{
struct point y = {0,0};
inputPoint(&y);
output(y);
}
Struct point inputPoint(struct point *p)
{
scanf("%d", &(p->x));
scanf("%d", &(p->y));
return p;
}
#include<stdio.h>
struct point {
int x;
int y
};
struct point *getStruct(struct point*);
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));
return 0;
}
struct point *getSturct(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", p->x, p->y);
}
6 15
6, 15
6, 15
网友评论