1, 利用do-while语句实现itoa(数字-->字符串)函数:/*将整数转换为字符串*/
/*将数组作为函数返回值得第一种方法:返回一个指针*/
char* itoa(int n)
{
char s[5];
int sign, i;
if((sign = n)<0)
n = -n;
i = 0;
do
{
s[i++] = n % 10 + '0';
}while((n/=10)>0);/*注意,do-while语句中的while后要有分号;*/
if(sign<0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
return s;
}
/*将数组作为函数返回值得第二种方法:将数组作为函数参数传递*/
/*实质上,就是按指针地址引用传递的一种思想*/
/*数组名作为函数参数传递其实上就是传递数组首地址(指针)*/
void itoa(int n, char s[])
{
int i, sign;
if((sign = n)<0)
n = -n;
i = 0;
do
{
s[i++] = n % 10 + '0';
}while((n/=10)>0);
if(sign<0)
s[i++] = '-';
reverse(s);
}
2,C语言之将数组作为返回值传递出函数:
写一个函数int *MakeArray(int c),生成大小为c的一个数组,返回指向这个数组的指针。
在函数内定义一个整形指针p,将数组名赋给p。直接的return array;是不行的,编译器给出警告信息。另外,array++的写法也不通。
开始,在函数中定义数组并初始化为1,2,3,……等等。函数中输出数组的值,无论是用array[i]的方式还是*p++的方式,都正确。
但是,将数组名通过指针传出函数之外,并在main()中用一个整形指针p存储,在用*p++访问,却总是不正确,都是垃圾值。
数组大概也有其生存周期。在函数int *MakeArray(int c)中定义数组,那么此数组的生存周期只在函数体内。离开函数后,此数组内容已被系统销毁。虽然通过指针传出并访问数组地址,但是其中的值已经不存在了。
所以,在main()函数用*p++访问,打印出来的总是别的值。
代码在此:
/*
how to return an array? try it.
*/
#define DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int *MakeArray(int c)
{
if(c<1)
{return 0;}
else
{
int array[c];
puts("\ninput the array data:\n");
int i;
for(i=0;i<c;i++)
{scanf("%d",&array[i]);}
#ifdef DEBUG
puts("\noutput in function(array[i]):\n");
for(i=0;i<c;i++)
{printf("%d\n",array[i]);}
#endif
int *p=array;
#ifdef DEBUG
printf("\np is %d,array is %d.\n",p,array);
//for(i=0;i<c;i++)
//{
//printf("array pointer %10d stored %10d.\n",array,*array);
//array++;//invalid code here...
//}//end for
puts("\noutput in function(*p++):\n");
for(i=0;i<c;i++)
{
printf("pointer %10d stored %10d.\n",p,*p);
p++;
}
p=array;
#endif
return p;
}
}//end
main()
{
puts("\ninput the length of your array:\n");
int c;
scanf("%d",&c);
int *p=MakeArray(c);
#ifdef DEBUG
printf("\nthe pointer got from function is %d.\n",p);
#endif
puts("\nmain output:\n");
int i;
for(i=0;i<c;i++)
//{printf("%d\n",p[i]);}
//{printf("%d\n",*(p--));}
{
printf("pointer %10d stored %10d.\n",p,*p);
p++;
}//end for
}//end main
/*
[Linker error] undefined reference to `WinMain@16 '
main function error
*/
/*
Output here:
D:\>return1array.exe
input the length of your array:
3
input the array data:
1
2
3
output in function(array[i]):
1
2
3
p is 2293504,array is 2293504.
output in function(*p++):
pointer 2293504 stored 1.
pointer 2293508 stored 2.
pointer 2293512 stored 3.
the pointer got from function is 2293504.
main output:
pointer 2293504 stored 2009143661.
pointer 2293508 stored 2009267360.
pointer 2293512 stored 2009143487.
D:\>
So now,you can see it clearly.
*/
数组可以以指针的方式返回,或者可以通过按指针(或数组名)作为函数参数传递,间接地改变数组的值,并输出。
实质上,数组名作为函数参数传递其实上就是传递数组首地址(指针)
#include <stdio.h>
int* fun(int*a,int b);
int main()
{
int a[5]= { 1,2,3,4 } ,*ptr,i;
ptr=fun(a,4);
for(i=0;i<4;i++)
printf("ptr[%d]=%d\n",i,ptr[i]);
return 0 ;
}
int* fun(int*a,int b)
{ int i ;
for(i=0;i<b;i++)
a[i]+=1 ;
return a ;
}
3,此外,在C#中,可以直接返回一个数组类型的值,这与C是不同的!
public int[] randques(){
int[] arr=new int[10]{0,1,2,3,4,5,6,7,8,9};
return arr;
}
//这样就返回的就是10个元素的数组
/*申明任意一长度为10的整形数组来接收这个方法的返回值即可*/
314



被折叠的 条评论
为什么被折叠?



