标题: 结构体小结 [打印本页]

作者: xiaojuan    时间: 2014-9-17 14:32
标题: 结构体小结
#include<stdio.h>

//第一种定义方式
struct Student  //定义一个结构体,类型定义
{
int age;  //内部为各结构体所包含的内容
float score;
char sex;
};     //分号不可以少

//第二种定义方式   不太好,只管用一次
struct Student
{
int age;  
float score;
char sex;
}st;   //定义好了变量名
//第三种方式   
struct Student
{
int age;  
float score;
char sex;
}st3;
int main (void)
{
struct Student st = {80, 66.6 'f'};//结构体赋值; //对变量进行类型选择,是结构体类型,st是结构体变量的名字
struct Student st1;
// st = {80, 66.6 'f'};这样写错误的

st1.age = 10;
st1.score = 12; //定义过后的只可以单个赋值  ‘ .’表示到结构体某一位下一位进行赋值
printf ("%d %lf %c",st.age, st.score, st.srx);
return 0;
}


作者: xiaojuan    时间: 2014-9-17 14:32

#include<stdio.h>
//结构体:把一些基本类型数据组合在一起形成的一个新的复合数据类型叫做结构体
//为什么需要结构体:为了表示一些复杂的事物,而普通的基本类型不可以满足
//第一种定义方式
struct Student  //定义一个结构体,类型定义,不是定义变量
{
int age;  //内部为各结构体所包含的内容
float score;
char sex;
};     //分号不可以少

//第二种定义方式   不太好,只管用一次
struct Student
{
int age;
float score;
char sex;
}st;   //定义好了变量名
//第三种方式  
struct
{
int age;
float score;
char sex;
}st3;
int main (void)
{
struct Student st = {80, 66.6 'f'};//结构体变量//结构体赋值; //对变量进行类型选择,是结构体类型,st是结构体变量的名字
struct Student st1;
// st = {80, 66.6 'f'};这样写错误的
//输出形式:结构体变量名.成员名        先编译下写名字.自动弹出课选择的成员名
st1.age = 10;
st1.score = 12; //定义过后的只可以单个赋值  ‘ .’表示到结构体某一位下一位进行赋值
//指针提前方法
struct Student * ps = &st;  //不可以不写&,因为指针用的都是地址
ps->age = 99; //计算机内部转换为(*ps).age
printf  printf ("%d %lf %c",st.age, st.score, st.srx);
("%d %lf %c",st.age, st.score, st.srx);
return 0;
}




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1