码迷,mamicode.com
首页 > 编程语言 > 详细

C语言基础_结构体

时间:2015-08-05 21:40:09      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

一、结构体

1)声明
  struct 用来声明结构体
  作用:管理多个数据信息

struct student{
    int num;  //成员变量之间用;隔开
    int age;
    char name[30];
    float score;
}Student;//分号不要忘记

2) 初始化

  1、设置初始值使用{}
  2、按照成员变量的顺序赋值
  3、可以不设置信息,使用{0}

    Student stu1={1,18,"luofeng",90.0};
    Student stu2={2,81,"sunjude",99.9};
    Student stu3={0};

  4、相同结构体类型变量可以相互赋值[数组名是不可以的] 

stu3 = stu1;

  5、结构体变量定义以后,不能整体赋值,只能给成员变量单独赋值

strcpy(stu2.name, "canglaoshi");
printf("%d,%d,%s,%.2f",stu1.num,stu1.age,stu1.name,stu1.score);

3)typedef 原类型名 新类型名   

  1、 typedef struct student Animal; //结构体重命名

typedef float  ok;
int main(int argc, const char * argv[]) { 
    ok a= 0.89;
    printf("%f\n",a);
    return 0;  
}

4)局部变量

  1、在函数内定义,整个函数内有效

  2、作用域:在{}内有效,出了{}就被释放,不能再使用

  3、不同的函数中,可以定义相同的变量,每个函数都有自己的作用域

  4、在某代码段中定义,只能在代码段中使用,循环,分支

  5、在函数中定义了变量,然后再代码段中也定义了相同的变量[在代码段中,以代码段内的变量使用,在代码段外,以函数内的变量使用]

5)全局变量

  1、函数外定义,程序运行结束释放

  2、从全局变量定义的位置开始,下面的程序中都能使用全局变量

  3、全局变量和局部变量可以重命名

  4、设置初始值使用{}, 可以不设置信息,使用{0}

  5、按照成员变量的顺序赋值

6) 结构化数组

  1、数组的元素都是结构体类型

typedef struct student{
    char name[20];
    int score;
    int age;
}Student;
int main(int argc, const char * argv[]) {
Student stu[3]={{"dawang",90,38},{"xiaowang",100,28},{"wangzha",80,18}};
    Student max = {0};
    Student min = stu[0];
    for (int i= 0; i<3; i++) {
        if (max.score < stu[i].score) {
            max = stu[i];
        }
        if (min.score > stu[i].score) {
            min = stu[i];
        }
    }
     printf("%s %d %d\n",max.name,max.score,max.age);
     printf("%s %d %d\n",min.name,min.score,min.age);
return 0;
}

  2、按成绩排序

typedef struct student{
    char name[20];
    int score;
    int age;
}Student;
int main(int argc, const char * argv[]) {
Student stu[3]={{"dawang",90,38},{"xiaowang",100,28},{"wangzha",80,18}};
for (int i = 0; i < 2; i++) {
        for (int j =0 ; j<2-i; i++) {
            if (stu[j].score < stu[j+1].score) {
                Student temp = {0};
                temp = stu[j];
                stu[j] = stu[j+1];
                stu[j+1] = temp;
            }
        }
}
for (int i= 0; i < 3; i++) { printf("%s %d %d\n",stu[i].name,stu[i].score,stu[i].age); } return 0; }

7) 结构体嵌套

typedef struct Date{
    int year;
    int month;
    int day;   
}Date;
typedef struct student{
    char name[20];
    int score;
    int age;
    Date birthday;
}Student;

int main(int argc, const char * argv[]) {
 Student stu = {"zhangsan",88,23,{2003,11,11}};
    stu.birthday.year = 2012;
    printf("%d,%d,%d",stu.birthday.year,stu.birthday.month,stu.birthday.day);
return 0;
}

8)函数调用结构体

  1、.h里代码

typedef struct teacher{
    
    char name[30];
    int number;
    int age;
    
}Teacher;

void tiger(Teacher t1,Teacher t2);//形参声明结构体变量
void blog(Teacher tea[],int n);

  2、.m里代码

void tiger(Teacher t1,Teacher t2)
{
    Teacher max = t1.age > t2.age ? t1:t2;
    printf("%s %d %d\n",max.name,max.number,max.age);
}

void blog(Teacher tea[],int n)
{
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-1-i; j++) {
            if (tea[j].age< tea[j+1].age) {
                Teacher temp;
                temp = tea[j];
                tea[j] = tea[j+1];
                tea[j+1] = temp;
                
            }
        }
    }
    for (int i=0; i<n;i++) {
        printf("%s %d %d\n",tea[i].name,tea[i].number,tea[i].age);
    }
}

  3、main.m里代码

    Teacher tiger1 = {"haha",2,23};
    Teacher tiger2 = {"hehe",3,25};
    tiger(tiger1, tiger2);
    
    Teacher tea[]={{"dawang",90,38},{"xiaowang",100,28},{"wangzha",80,18}};
    blog(tea, 3);

 

C语言基础_结构体

标签:

原文地址:http://www.cnblogs.com/ChinaTiger/p/4704725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!