我是靠谱客的博主 淡然黑猫,最近开发中收集的这篇文章主要介绍结构体做函数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include "stdio.h"
#include <stdlib.h>
#include <string.h>

//定义结构体类型
typedef struct T2
{
	char *name;
	int age;
	int score;
} T2;
//打印
void printfage(T2 *Array,int num)
{
	int i = 0;

	for(i=0;i<num;i++)
	{
		printf("Array[%d] age is %d.n",i,Array[i].age);
	}
}
//排序
void sortage(T2 *Array,int num)
{
	int i = 0 , j = 0;

	for(i=0;i<num;i++)
	{
		for(j=i;j<num;j++)
		{
			if(Array[i].age<Array[j].age)
			{
				int temp = Array[i].age;
				Array[i].age = Array[j].age;
				Array[j].age = temp;
			}
		}
	}	
}
//创建空间
T2* creat(int num)
{
	T2* temp =NULL;
	int i = 0;

	temp = (T2*)malloc(sizeof(T2) * num);

	for(i=0;i<num;i++)
	{
		printf("Please input Array[%d] age:",i);
		scanf("%d",&(temp[i].age));
	}

	return temp;
}
//释放空间
void freecreat(T2* p)
{
	if(p != NULL)
	{
		free(p);
	}
}

void main()
{
	T2* ptemp = NULL;
	int num = 3;
	int i=0;

	ptemp = creat(num);

	printfage(ptemp,num);

	sortage(ptemp,num);
	printfage(ptemp,num);

	freecreat(ptemp);

	system("pause");
}

最后

以上就是淡然黑猫为你收集整理的结构体做函数的全部内容,希望文章能够帮你解决结构体做函数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部