结构体\结构体数组

简介: 结构体\结构体数组
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
//定义结构体
typedef struct Date
{
  int year;
  int month;
  int day;
}Date;
 
typedef struct
{
  int id;
  char name[20];
  Date birthday;
  char sex;
  double score;
}Student;
 
int main()
{
  //定义结构体数组并初始化
  Student st[3] = { {1001,"JACK",{1992,5,21},'F',83},{1002,"LUCY",{1993,6,10},'M',66} };
  int i;
 
  //给最后一组数据赋值——原来的初值默认为零
  st[2].id = 1003;
  strcpy(st[2].name, "zyq");
  st[2].birthday.year = 2005;
  st[2].birthday.month = 6;
  st[2].birthday.day = 10;
  st[2].sex = 'M';
  st[2].score = 100;
 
  //用第二、三种方法访问结构体成员
  for (i = 0; i < 3; i++)
    printf("%d,%s, %d.%d.%d ,%c,%.2f\n", (st + i)->id, (st + i)->name, (st + i)->birthday.year, (*(st + i)).birthday.month, (st + i)->birthday.day, (st + i)->sex, (st + i)->score);
 
  system("pause");
  return 0;
}


目录
相关文章
|
11月前
【结构体】
【结构体】
27 0
|
11月前
|
存储 C语言
C 结构体
C 结构体。
30 0
|
4月前
|
存储 算法 数据安全/隐私保护
结构体
结构体
47 1
|
4月前
|
存储 C语言
使用结构体数组
使用结构体数组
40 0
|
3月前
初识结构体
初识结构体
39 5
|
3月前
|
存储 算法 C++
C++结构体
C++结构体
|
4月前
|
算法 C语言
结构体相关知识
结构体相关知识
|
4月前
|
算法 程序员 C++
|
编译器 C++
|
存储 C++