返回一个字符串数组 [英] return a string array

查看:238
本文介绍了返回一个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i想要返回一个字符串

hi

i would like to return a string

展开 | < span class =codeLinkonclick =selectAll(this);>选择 | 换行 | 行号

推荐答案




collinm写道:


collinm wrote:


我想返回一个字符串
[...返回指向'auto''变量的指针...]
hi

i would like to return a string
[... returns a pointer to an `auto'' variable ...]




这是comp.lang.c中的问题7.5经常

问答(FAQ)列表

http://www.eskim o.com/~scs/C-faq/top.html


-
Er ********* @sun.com



This is Question 7.5 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com


文章< 11*********************@o13g2000cwo.googlegroups。 com>,

collinm< co ***** @ laboiteaprog.com>写道:
In article <11*********************@o13g2000cwo.googlegroups. com>,
collinm <co*****@laboiteaprog.com> wrote:
我想返回一个字符串
char * readFile(char * tmp_dir_led,char * directory,char * filename)
{
FILE * fp;
char line [LINE_MAX];
返回行;
}
i would like to return a string char *readFile(char *tmp_dir_led, char *directory, char *filename)
{
FILE *fp;
char line[LINE_MAX]; return line;
}




行是存储类自动的变量。通常这意味着

它将在堆栈上分配。然后你返回一行,这将是一个数组无声地衰减,成为指向数组的第一个元数据的指针。但是当该指针值在例行程序外使用时,它指向逻辑上不再存在的东西

(自动存储)。


如果要返回生成的字符数组,则必须为数组提供
malloc()[或等效]空间并返回它。并且

记得以后释放()空格。

-

任何足够先进的bug都与功能无法区分。

- Rich Kulawiec



line is an variable of storage class "automatic". Usually that means
it will be allocated on the stack. You then return line, which will
silently decay from being an array to become a pointer to the first
element of the array. But when that pointer value is used outside
the routine, it points to something that logically no longer exists
(the automatic storage.)

If you want to return a generated character array, you will have to
malloc() [or equivilent] space for the array and return it. And
remember to free() the space afterwards.
--
Any sufficiently advanced bug is indistinguishable from a feature.
-- Rich Kulawiec


" collinm" <共***** @ laboiteaprog.com>写道:
"collinm" <co*****@laboiteaprog.com> wrote:
char * readFile(char * tmp_dir_led,char * directory,char * filename)
{
FILE * fp;
char line [ LINE_MAX];
snprintf(tmp_mnt_dir_led,sizeof(tmp_mnt_dir_led),"%s /%s",目录,文件名);
char *readFile(char *tmp_dir_led, char *directory, char *filename)
{
FILE *fp;
char line[LINE_MAX];
snprintf(tmp_mnt_dir_led, sizeof(tmp_mnt_dir_led),"%s/%s", directory,filename);




除了答案通过Eric,您需要意识到这也是一个

错误。指针对象上的sizeof给出了指针的大小

本身,而不是它指向的区域。很可能这意味着即使你用b = b
解决了返回值的问题,你也无法打开文件,因为文件名被截断了;如果你传入一个

指针到一个过短的char区域(或者有过大的
指针类型......),你可能会通过写

名称缓冲区的结尾。


Richard



Apart from the answer by Eric, you need to realise that this is also a
bug. sizeof on a pointer object gives you the size of the pointer
itself, not of the area it points at. Most likely this means that even
if you solve the problem with the return value, you will not be able to
open the file because the file name gets truncated; if you pass in a
pointer to an excessively short char area (or have excessively large
pointer types...), you may cause undefined behaviour by writing past the
end of the name buffer.

Richard


这篇关于返回一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆