函数返回问题

pzh508 2003-04-03 03:22:11
有一个函数,该函数必须返回数组的内容,该怎么实现?
...全文
29 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzhaohan 2003-04-03
  • 打赏
  • 举报
回复
其实本来函数返回值就有很多方法,return是一种,通过全局变量和静态变量也可以,当然也可以通过入口参数是指针来返回值。你把一根指针作为入口参数,函数执行完,继续操纵这根指针就行。肯定不能返回再栈上分配的内存,但是也可以返回再堆上分配的内存。
pzh508 2003-04-03
  • 打赏
  • 举报
回复
to netcpp(我在故我思,疯狂修炼中...)
谢谢,我明白了,难怪我老是输出单个字符,原来我不小心将test 写成了*test;
netcpp 2003-04-03
  • 打赏
  • 举报
回复
上面的代码在VC6下有警告,warning C4172: returning address of local variable or temporary
When returning the address of a variable, you should return the address of a global variable or a dynamically allocated variable, which will continue to exist after the function ends.
上面的_goolooloo_说的对,使用指针和引用,在函数内部修改外部数据,这里只需修改一个地方就可以了,将数组声明为全局的,这里用new 不好
#include <iostream.h>
#include <string.h>

char *getstr();
void main()
{
char *test;
test=getstr();

cout<< test<<endl;// 如果要输出整个字符数组,这里使用test,不是*test


}

char *getstr()
{
static char sztemp[]="this is test"; // 声明为static
return sztemp;
}
sprbeyond 2003-04-03
  • 打赏
  • 举报
回复
sztemp是局部变量,在函数返回后堆栈就被释放了。利用返回的地址中的内容是很危险的。
kajing 2003-04-03
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <string.h>

void getstr(char *str);
main()
{
char test[100];
getstr(test);
cout<<*test<<endl;

}

void getstr(char *str)
{
sprintf(str,"this is test");
}
_goolooloo_ 2003-04-03
  • 打赏
  • 举报
回复
你在进行一项危险的操作.
返回一个局部变量的地址,虽然有可能得到函数内局部变量的值,
但是,他的存储空间已被标记为"未使用",因此,这个值是不安全的,有可能会被其他操作修改.
如果要修改你的程序的话,建议修改getstr()的接口。
在主程序中定义字符指针(或数组),作为getstr的参数传进去,
在getstr内部对参数的内存区操作,不带返回值或返回操作成功标志。
不过你用C++,建议你用String类。
fangrk 2003-04-03
  • 打赏
  • 举报
回复
void fun(const char* cc)
{
static char saved[50];
strcpy(saved,cc);
return saved;
}
pzh508 2003-04-03
  • 打赏
  • 举报
回复
关键是怎么返回一整个字符串而不是单个字符
pzh508 2003-04-03
  • 打赏
  • 举报
回复
to coffeebike(咖啡色BIKE)
什么意思??sztemp是要在getchr函数里的啊
coffeebike 2003-04-03
  • 打赏
  • 举报
回复
就改一句话:
#include <iostream.h>
#include <string.h>

char *getstr();
main()
{
char *test = NULL;
test = getstr();
cout << test << endl;

}

char *getstr()
{
char *sztemp = "this is test";
return sztemp;
}


char *sztemp = "this is test";//这样申明就可以了啊
Behard 2003-04-03
  • 打赏
  • 举报
回复
Kao,怎么会这样呢?
main()
{
int *p=IntArray();
for ( int i=0; i<N; i++ )
count << p[i] << "\n";
while(!kbhit()); gethc();
}
Behard 2003-04-03
  • 打赏
  • 举报
回复
可以使用 rtdb(东临碣石) 的做法,或者
const int N=10;
int *IntArray();

main()
{
int *p;
for ( int i=0; i<N; i++ )
count << p[i] << "\n";
while(!kbhit()); gethc();
}

int *IntArray()
{
static int iArray[N]={10,10,10};
return iArray;
}
pzh508 2003-04-03
  • 打赏
  • 举报
回复
to liuheliaoshi7879(虾米)
按照你这样的做法,只能返回数组的第一个值啊
浮生若梦 2003-04-03
  • 打赏
  • 举报
回复
总觉得指针不好弄
pzh508 2003-04-03
  • 打赏
  • 举报
回复
哦,错了,我意思是这样
请看下面的代码
#include <iostream.h>
#include <string.h>

char *getstr();
main()
{
char *test;
test=getstr();
cout<<*test<<endl;

}

char *getstr()
{
char sztemp[]="this is test";
return sztemp;
}
这样返回的总是sztemp的第一字符t,但我想返回整个字符串,该怎么办?
rtdb 2003-04-03
  • 打赏
  • 举报
回复
我一般是传数组进去修改。

这样符合内存管理中的谁分配谁删除的原则。
prettynacl 2003-04-03
  • 打赏
  • 举报
回复
是啊,返回首地址,就可以得到数组内容了。
要不就以数组做参数.
liuheliaoshi7879 2003-04-03
  • 打赏
  • 举报
回复
main()
{int *p;
p=re();
cout<<p;
}

int *re()
{int b[5]={1,2,3,4,5};
return(b);
}


只需让函数的返回值是数组的首地址
wy121 2003-04-03
  • 打赏
  • 举报
回复
返里数组地址不行吗?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧