avatar

松果工作室

欢迎光临

  • 首页
  • freeRTOS
  • ESP
  • 开发手册
  • 快速笔记
  • 个人收藏
  • 工具
Home 结构体创建的 3 种方式
文章

结构体创建的 3 种方式

Posted 2024-06-7 Updated 2024-06- 7
By YCP
6~8 min read

标准方式

#include <stdio.h>
struct student //结构体类型的说明与定义分开。声明
{
int age;  /*年龄*/
float score; /*分数*/
char sex;   /*性别*/
};
int main ()
{
struct student a={ 20,79,'f'}; //定义
printf("年龄:%d 分数:%.2f 性别:%c\n", a.age, a.score, a.sex );
return 0;

不环保方式

#include <stdio.h>
struct student /*声明时直接定义*/
{
int age;  /*年龄*/
float score;  /*分数*/
char sex;   /*性别*/
/*这种方式不环保,只能用一次*/
} a={21,80,'n'};
int main ()
{
printf("年龄:%d 分数:%.2f 性别:%c\n", a.age, a.score, a.sex );

一次性方式

#include <stdio.h>
struct{
  int age;
  float score;
  char sex;
} t={21,79,'f'};
int main(){
  printf("年龄:%d 分数:%f 性别:%c\n", t.age, t.score, t.sex);
  return 0;
}
Others
License:  CC BY 4.0
Share

Further Reading

OLDER

CH573(三) 代码结构

NEWER

(一) 三极管面向应用的原理

Recently Updated

  • ESP32(八) 简单的webserver
  • ESP32(七) NVS
  • ESP32(四) STA & AP
  • 多级菜单
  • ESP32(五) ESP32 OTA

Trending Tags

WCH Linux Elec freeRTOS STM ESP Flutter Others SwiftUI

Contents

©2025 松果工作室. Some rights reserved.

Using the Halo theme Chirpy