#include<stdio.h>
#include<stdlib.h>
float * Input(int&len)
{
int length
int i
float *a
printf("Enter a number to sort numbers:\n")
scanf("%d",&length)
len = length
a=(float *)malloc(length*sizeof(float))
printf("Enter the number %d (Digital separated by whitespace or the end of line):\n",length)
for(i=0i<lengthi++)
{
scanf("%f",&a[i])
}
return a
}
void Sort(float * a, int len)
{
int i,j,length
length = len
for(i=0i<length-1i++)
for(j=0j<length-1-ij++)
if(a[j]>a[j+1])
{
a[j]=a[j]+a[j+1]
a[j+1]=a[j]-a[j+1]
a[j]=a[j]-a[j+1]
}
}
void main()
{
float *date = NULL
int len
int i
date = Input(len)
Sort(date, len)
printf("This %d number from small to large order is the order of:\n",len)
for(i=0i<leni++)
printf("%-7.2f\n",date[i])
free(date)
}
我改了一下你的程序,不太好看,只是给你看看怎么返回指针。
首先,你不应该返回局部变量的指针,因为局部变量在离开函数后就会被销毁。如果你想这样返回一个数组的话,你应该malloc出一个数组来返回。至于应该return什么,return
(malloc出来的那个指针)就可以了。
例如
int**
foo()
{
int
**p
=
(int
**)malloc(sizeof(int
*)*10)
...
...
...
return
p
}
这个p指向一个内存段,这个内存段存有十个int*类型的元素。