C语言入门-结构类型

这篇博客介绍了C语言中的结构类型,包括声明结构类型、结构在函数内外的作用域、结构的声明形式、初始化方式、结构与函数的交互,特别是结构作为函数参数和结构指针的使用。强调了结构指针在传递和访问结构成员时的重要性。
摘要由CSDN通过智能技术生成

一、声明结构类型

#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;
}

二、在函数内/外 ?

  1. 和本地变量一样,在函数内部声明的结构类型只能在函数内部使用
  2. 所以通常在函数外部声明结果类型,这样就可以被多个函数使用了
#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值