struct score { char str[10]; int num1; double num2; }; //定义完了不要忘了加分号!
若是这个结构类型名要在多个函数内使用,就把它定义在全部函数外面,就像定义全局变量那样ios
前面要有 struct 前缀,后面跟着结构体类型名 score,而后花括号内写结构体的成分函数
以下spa
#include<iostream> using namespace std; struct a { char str[10]; int num1; double num2; }; int main(void) { a student = { "小明",1,2 }; cout << student.str << student.num1 << student.num2 << endl; return 0; }
以下,结构体的初始化能够在定义类型名的时候先给成分初始化code
struct score { char name[100]; //姓名 unsigned long int number; //学号 double score[2]; char course[2][100] = { {"物理"},{"数学"} }; };
其余没有被初始化的还能够在定义结构体类型变量的时候初始化,以下数学
int main(void) { score student[NUM] = { {"肖战",2000,20,30}, {"古力扎娜",2001,20,30}, {"尼格买提",2002,20,30}, {"撒贝宁",2003,20,30}, {"赵丽颖",2004,20,30}, {"迪丽热巴",2005,20,30} }; return 0; }
在main函数中定义 score 类型的变量 student 时能够初始化在定义 struct 类型名 score 时没有初始化的成分io