美文网首页
结构:结构与函数

结构:结构与函数

作者: 爱生活_更爱挺自己 | 来源:发表于2020-11-06 08:32 被阅读0次

结构作为函数参数

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

相关文章

  • 结构:结构与函数

    结构作为函数参数 int numberOfDays(struct date d); 整个结构可以作为参数的值传入函...

  • Day10

    指针 指针与函数 练习回调函数 结构体 基本概念 结构体变量初始化 定义结构体变量 结构体变量作用域结论; 和变量...

  • C语言的结构体(篇章之二)

    结构体与函数 一、结构体作为函数的参数 【一】、传值调用方式在函数之间直接传递结构体类型的数据——传值调用方式。当...

  • Python基础(三)——程序结构

    与c语言类似,Python的程序结构包括分支结构、循环结构和函数。 1.分支结构 语句为if....elif......

  • c++数据结构

    函数与数据共存 C++中首先允许结构中可以定义函数,这些函数称为成员函数,形式如下: 可以像C语言中结构变量使用结...

  • 控制语句与流程

    本章内容包括 if选择结构 for循环结构 for 与 range 函数的使用 while循环的使用 compre...

  • Swift 2 学习笔记 11.结构体

    课程来自慕课网liuyubobobo老师 结构体 结构体基础 结构体之构造函数 结构体之可失败的构造函数 在结构体...

  • Python - 语法

    数据结构 函数 高级特性 函数式编程 数据结构

  • Swift-结构体

    结构体基础 结构体构造函数 结构体内的函数 重点 引用类型: Array、Set、Dictionary / Str...

  • 6.结构体相关

    一 C 结构体和结构体指针 eg1: 一 结构体里面定义函数 通过结构体指针访问结构体里面定义的函数。 eg2:

网友评论

      本文标题:结构:结构与函数

      本文链接:https://www.haomeiwen.com/subject/gvizvktx.html