ios开发之c语言基础-结构体

//

//  main.m

//  C7- 结构体

//

//  Created by dllo on 15/10/14.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>



// 声明一个结构体


//关键字(struct) 结构体名(student){


//结构体成员...


//}

//

//struct student {

//

//    char name[20];

//    char sex;

//    float score;

//    int age;

//};

struct rect {

    char name[20];

    float len;

    float width;

    double area;

};


//声明并且起名字,struct student student代替

 typedef struct student {    int score;

    int age;

    char name[20];

}student;

//结构体作为函数参数


//简单类型的结构体作为函数形参

void testStruct(struct student stu)

{

    printf("%s",stu.name);

    

}

//数组类型的结构体变量做函数形参

void testStructArr(struct student stu[])

{

    printf("%s",stu[1].name);

    

}

//为已经存在的类型命名新的名字

//Yuhao 代替 int struct student student代替

 typedef int HuHao;

//typedef struct student student;

int main(int argc, const char * argv[]) {

    

    HuHao a = 0;

   // int b = 0;

    printf("%d",a);

    //定义一个结构体变量

    //变量类型 变量名 初值

    //注意,变量类型为: struct student

    //变量名为: stu

//    struct student stu = {0};

//                                                  // 注意,初值顺序与声明匹配

//    struct student stu1 = {"yuhao",'n',99.9,23};

//    struct rectangle rec1 = {5,6,30};

//    

    

    //访问结构体变量的成员

//    stu.age = 25;

//    printf("%d ", stu.age);

//    

    // 结构体可以整体赋值

//    stu = stu1;

//    printf("%d ",stu.age);

//    

    

   // strcpy(stu.name, "丝袜");

    //注意数组不可以整体赋值,只可以通过for ,或者字符串函数strcpy

    

    //3个学生,编程找出分数最高的以及年纪最小的

//    int maxscore = 0;

   // int minage = 70;

//    struct student stu1 = {70, 20,"yuhao"};

//    struct student stu2 = {80, 20,"yuaho"};

//    struct student stu3 = {80, 40,"yuao"};

//    

//    if (stu1.score > maxscore) {

//        maxscore = stu1.score;

//    }

//    if (stu2.score > maxscore) {

//        maxscore = stu2.score;

//    }

//    if (stu3.score > maxscore) {

//        maxscore = stu3.score;

//    }

   // printf("%d %d", maxscore, minage);

//    

//    maxscore = stu1.score > stu2.score ? stu1.score : stu2.score;

//    maxscore = maxscore > stu3.score ? maxscore : stu3.score;

//    struct student maxStu = {0};

    //比较分数,但返回整体信息-因为结构体变量可以整体赋值;

//    maxStu = stu1.score > stu2.score ? stu1 : stu2;

//    maxStu = maxStu.score > stu3.score ? maxStu : stu3;

    //输出分数最大的学生的所有信息

//    printf("%d %d %s ",maxStu.score, maxStu.age, maxStu.name);

//    

//    struct student minageStu = {0};

//    minageStu = stu1.age < stu2.age ?  stu1 : stu2;

//    minageStu = minageStu.age < stu3.age ? minageStu : stu3;

//    printf("%d %d %s ",minageStu.score,minageStu.age,minageStu.name);

//

//

//    //结构体数组

//    struct student stu[2000] = {0};

//    

//    //将第二名同学的年龄赋值为80

//    stu[1].age = 80;

////    

//    struct student stu[3] = {

//        {70, 20,"yuhao"},

//        {80, 20,"yusaho"},

//        {90, 40,"yusao"}

//        

//    };

//    

    // 5个同学保存在结构体数组中,编程查找成绩最高者,输出该学生的全部信息

//    int max = 0;

  //  struct student maxscore = {0};

//    struct student stu[5] = {

//        {70, 20,"yuhao"},

//        {80, 10,"yusaho"},

//        {90, 40,"yussao"},

//        {91, 38,"yausao"},

//        {97, 41,"yasusao"}

//    };

//    for (int i = 0; i < 5; i++) {

    /*    minageStu = stu1.age < stu2.age ?  stu1 : stu2;

        minageStu = minageStu.age < stu3.age ? minageStu : stu3;

    printf("%d %d %s ",minageStu.score,minageStu.age,minageStu.name);

     */

    //这种方法只能输出一个最大值

    

//        if (stu[i].score >max){

//            max = stu[i].score;

//        }

//    }

    

    

//    for (int i = 0; i < 5; i++) {

//        if (stu[i].score == max){

//            printf("%d %d %s",stu[i].score,stu[i].age,stu[i].name);

//        }

//    }

//    

    

    //对上面的5位同学鞍成绩从高到低排序,并输出

   // struct student maopaostu[5] ={0};

   // struct student a = {0};

//    struct student stu[5] = {

//        {70, 20,"yuhao"},

//        {80, 10,"yusaho"},

//        {90, 40,"yussao"},

//        {91, 38,"yausao"},

//        {97, 41,"yasusao"}

//    };

    //冒泡排序

//    for (int i = 0; i < 5 ; i++) {

//        for (int j = 0; j < 5 - i - 1; j++) {

//            if (stu[j].score  < stu[j + 1].score) {

//                struct student stu1 = {0};

//                stu1 = stu[j];

//                stu[j] = stu[j + 1];

//                stu[j + 1] = stu1;

//            }

//        }

//    }

//    

//    for (int i = 0; i < 5 ; i++) {

//        maopaostu[i] = stu[i];

   // }

/*  结构体数组也不可以直接赋值

    strcpy(maopaostu, stu);这条语句是错误的,strcpy只能将含有相同类型的数组复制,结构体数组中含有字符串char,不可以直接复制

 */

//    for (int i = 0; i < 5; i++) {

//        printf("%d %d %s",maopaostu[i].score, maopaostu[i].age, maopaostu[i].name);

//        printf(" ");

//    }

//    printf(" ");

//    for (int i = 0; i < 5; i++) {

//        printf("%d %d %s",stu[i].score,stu[i].age,stu[i].name);

//        printf(" ");

//    }

//

  

//    struct student stu =  {70, 20,"yuhao"};

//    testStruct(stu);

//    struct student stu1[5] = {

//                {70, 20,"yuhao"},

//                {80, 10,"yusaho"},

//                {90, 40,"yussao"},

//                {91, 38,"yausao"},

//                {97, 41,"yasusao"}

//            };

//    testStructArr(stu1);

//

    

    long ret = sizeof(student);

    printf("%ld ", ret);

    //按照最大的类型字节分.(4字节对齐)内存存储原则:先按照最大的类型字节分,每个变量都很笨,他会填补上个变量空下来的内存,但是他也很笨,他以为自己和他是同一种类型,

    

    

    

    

    

    

    

    

    

    return 0;

}


原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043136.html