//n个整数排序并输出(函数实现),输入和输出在主函数实现#include<stdio.h>#include<stdlib.h>int*sort(int* p,int n){//这里用冒泡排序法实现从小到大的排序for(int i =0; i < n -1; i++){for(int j =0; j < n -1- i; j++){if(*(p + j)>*(p + j +1)){int t =*(p + j);*(p + j)=*(p + j +1);*(p + j +1)= t;}}}return p;}intmain(){//开辟动态存储空间int n;printf("Please enter the number of integers-->n:\n");scanf_s("%d",&n);int* p =(int*)malloc(n *sizeof(int));//开辟失败,内存不够,返回空指针NULL----注意这里if(p ==NULL){//打印错误原因的一个方式printf("ERROR\n");//Not enough space}//输入要操作的整数printf("Please enter the integer you want to operate\n");for(int i =0; i < n; i++){scanf_s("%d", p + i);}//原样输出printf("The original data is:\n");for(int i =0; i < n; i++){printf("%d\t",*(p+i));}printf("\n");//调用函数处理数据
p =sort(p, n);//输出处理后的结构printf("The operated data is:\n");for(int i =0; i < n; i++){printf("%d\t",*(p + i));}free(p);return0;}