代码案例(结构体,函数指针,指针函数,冒泡排序)

typedef struct
{
    char name[20];
    int age;
    float score;
}Stu;
#import <Foundation/Foundation.h>
//姓名升序
void sortByName(Stu *p , int count )
{
    for (int i = 0 ; i < count -1; i ++) {
        for (int j= 0 ; j < count -1-i; j ++) {
            if (strcmp((p + j)->name, (p + j + 1)->name) > 0 ) {
                Stu temp  = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }
    
}
//年龄
void sortByAge( Stu *p , int count )
{
    for (int i = 0 ; i < count -1; i ++) {
        for (int j= 0 ; j < count -1-i; j ++) {
            if ((p + j)->age > (p + j + 1)->age) {
                Stu temp  = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }

}
//成绩
void sortByScore(Stu *p , int count )
{
    for (int i = 0 ; i < count -1; i ++) {
        for (int j= 0 ; j < count -1-i; j ++) {
            if ((p + j)->score > (p + j+ 1)->score ) {
                Stu temp  = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }

}

void outPut(Stu *p , int count )
{
    for (int i = 0 ; i < count ; i ++) {
        printf("name = %s , age = %d , score = %.2f\n" , (p + i)->name,(p + i)->age , (p + i)->score);
    }
    
}
typedef void (*FUN) (Stu *p , int count);
void student (FUN func ,Stu *p , int count )
{
    func(p , count);
}
int main(int argc, const char * argv[])
{
    Stu stu[5] = {
        {"zhang",20,80},
        {"wang", 22,82},
        {"li",23,86},
        {"zhao",22,83},
        {"liu",20,89}
    };
    
    Stu *p = NULL;
    p = stu;
    student(outPut,p,5);
    student(sortByName,p, 5);
    student(outPut,p,5);
   // outPut(p, 5);
   // sortByName(p, 5);
  //  outPut(p, 5);
       return 0;
}

代码案例(结构体,函数指针,指针函数,冒泡排序)

时间: 2024-08-01 19:02:29

代码案例(结构体,函数指针,指针函数,冒泡排序)的相关文章

代码案例(结构体,函数指针,指针函数,冒泡排序) 修改

#import <Foundation/Foundation.h> typedef struct {     char name[20];     int age;     float score; }Stu; //建立字符串和函数之间的一一对应关系. typedef BOOL (*PStu) (Stu stu1,Stu stu2) ; typedef struct nameFunctionPair{     char name[20]; //存储函数对应的字符串     PStu funct

指向结构体变量的指针作函数参数

 /********************* * 指向结构体变量的指针作函数参数 * ***********************/ #include<stdio.h> #include<string.h> struct student {  int num;  char name[20];  //char *name;    //若定义为指针则应与下面 stu.name = "jacob"; 配对使用,交叉使用则会报错                 //

C语言结构体传值--&gt;通过指针进行传值

结构体的传值方法一共有三种形式,通过传递结构体,传递指针,传递结构体自身参数.传递指针的方式与另外两种方法最大的不同就是传递的实际上是结构体的地址,在传值的过程中,指针需要进过初始化分配内存(也就是使用malloc()函数分配空间给指针) 来看看以下代码: 有两个点需要注意: (1)在方法设置类型的时候 是一个struct Book 类型,同时还是一个指针的类型,可以说(struct Book && pointer类型) (2)在代码的32,33行处,声明了一个struct Book &a

通过一个函数,操作一个结构体,实现对应函数功能

指针结构体一直是我的盲点,所以今天有必要整“清理门户”.此种通过一个函数操作一个结构体,实现对应函数功能,用法十分巧妙,使用得当可以使得代码可移植性和易懂性大大的增加,有人说过“代码注释的最高境界是程序的自述,而不是双斜杠然后后面跟着中英文的注释”.哈哈,说远了,下面开始进入今天的加油站,补充体力了. 1 // 头文件 2 #include <stdio.h> 3 4 // 函数声明 5 typedef struct _halDeviceFuncs_t 6 { 7 void (*pfnInit

C++ 结构体与类指针

在上一章中, 我们已经了解到了普通指针的基础使用方式, 但是知道结构体与类中的指针是如何使用的吗? 就来介绍一下 如果本章的内容不适合你, 可以查看 C++指针目录 在结构体或类中, 指针访问其成员函数或变量通过 "->" 运算符或者看代码注释部分, 注释部分的操作不推荐: #include <iostream> #include <cstring> using namespace std; struct STRUCT { string hello; };

file结构体中private_data指针的疑惑【转】

本文转载自:http://www.cnblogs.com/pengdonglin137/p/3328984.html hi all and barry, 最近在学习字符设备驱动,不太明白private_data在字符驱动中的作用,我们在 驱动中添加一个设备结构体,然后定义了这个结构体的全局指针变量,接着我们就能在 驱动程序中使用这个指针了.我看到很多驱动程序中都把结构体指针付给private_data, 然后对private_data操作. 为什么要使用private_data,难道仅仅是避免使

C提高4 多维数组,结构体与二级指针

昨天的试题:原题见昨天作业题 [email protected]:~/high/day03$ cat main.c  /* 二级指针,绝佳训练题1 */ #include <stdio.h> #include <stdlib.h> #include <string.h> int spitspring(char *buf1,char *c,char **myp,int *total) { char *p_head =  buf1; char *p_tail =  buf1

C语言 指向结构体变量的指针

一个指向变量的指针表示的是占内存中起始位置 一个指向结构体的变量的指针表示的是这个结构体变量占内存中的起始位置,同样它也可以指向结构体变量数组 定义结构体变量的指针: //假设已有一个结构体名为Student struct Student *pStruct // 结构体类型 * 指针名; 通过指针来引用结构体中的成员,有三种方式 demo: 1 # include <stdio.h> 2 # include <stdlib.h> 3 /* 4 1.使用->引用结构体成员 5

c语言:通过指向结构体变量的指针变量输出结构体变量中成员的信息

通过指向结构体变量的指针变量输出结构体变量中成员的信息. 解:程序: #include<stdio.h> #include<string.h> int main() { struct Student { long int num; char name[20]; char sex[10]; float score; }; struct Student stu_1;//定义struct Student类型的变量stu_1 struct Student *p; p = &stu_