从函数返回字符串数组 [英] Returning a string array from a function
问题描述
我想从函数返回一个(任意长度)字符串数组,所以
调用数组之后我就是我有一个可以访问的字符串列表。
经过多次浏览互联网后,我在这里找到了一个相关的答案(由
Eric Sosman提供)创建一个指针数组并使用
,它看起来像:
void foo(char * sptr [],int items)
{
char buff [50];
while(--items> = 0)
{
sprintf(buff,foo%d,items);
sptr [items] = malloc(strlen(buff)+ 1);
if(sptr [ items] == NULL)
退出(0);
strcpy(sptr [items],buff);
}
}
int main(无效)
{
int total = 10;
char * p [total];
foo(p,total);
printf(p [1]);
返回0;
}
这是一种享受(我可以访问我的字符串,例如printf线)但是
如果我不知道我的清单要花多长时间,我会怎么做类似的事情
是什么?例如。 "总"只有在foo里面才能找到。
非常感谢,
亚当
-
Adam Richardson
Carpe Diem
" Adam" < ne ** @ snowstone.org.ukwrote in message
news:48 ************** @ snowstone.org.uk ...
我想从函数中返回一个(任意长度)字符串数组,所以
在调用数组后我有一个我可以访问的字符串列表。
经过多次浏览互联网后,我在这里找到了一个相关的答案(由
Eric Sosman)涉及创建一个指针数组并使用
,所以它看起来像:
void foo(char * sptr [],int items)
{
char buff [50];
while(--items> = 0)
{
sprintf(buff," foo%d",items);
sptr [items] = malloc(strlen(buff)+ 1 );
if(sptr [items] == NULL)
exit(0);
strcpy(sptr [items],buff) ;
}
}
int main(无效)
{
int total = 10;
char * p [total];
foo(p,total);
printf(p [1]);
返回0;
}
这是有效的对待(我可以访问我的字符串,例如printf线)但是
如果我不知道我的清单要花多长时间,我会怎么做类似的事情
是什么?例如。 "总"只有在foo里面才能找到。
非常感谢,
Adam
char * * foo(int items)
{
char ** answer;
char buff [100];
int i;
answer = malloc(items * sizeof(char *));
if(!answer)
goto error_exit;
/ *需要清理,不能有非空垃圾指针* /
for(i = 0; i< items; i ++)
回答[i] = 0;
for(i = 0; i< items; i ++)
{
sprintf(buff,foo%d,i):
回答[i] = malloc(stren(buff)+ 1);
if(!answer [i])
转到error_exit;
strcpy(回答[i],buff);
}
返回答案;
error_exit:
if(answer)
for(i = 0; i< items; i ++)
免费(回答[i]);
免费(回答);
/ *可能打印错误信息/终止于这里* /
返回0;
}
在文章< Mt ****************************** @ bt.com> ;,
Malcolm McLean< re ******* @ btinternet.comwrote:
>" Adam" < ne ** @ snowstone.org.ukwrote in message
新闻:48 ************** @ snowstone.org.uk ...
>我想从函数中返回一个(任意长度)字符串数组,以便在调用数组后我有一个可以访问的字符串列表。
answer = malloc(items * sizeof(char *));
if(!answer)
转到error_exit;
/ *需要清理,不能有非空垃圾指针* /
嗯。 Adam的问题并不像教科书的作业那样,你需要使用goto来降低代码质量,以便
降低对任何复制它的人的标记进行评级。
-
那时候我很年轻,但我也很朦胧。
- Christopher Priest
在消息< Wb ************************ ****** @ comcast.com>,Eric Sosman
写道:
Adam写道:
我想从一个函数返回一个(任意长度)字符串数组,所以
调用数组之后我有一个字符串列表我可以访问。
^^^^^功能
[剪辑讨论]
非常感谢您提供有用的信息。我会离开并做一些思考
关于最好的行动方案。 :)
您使用的是C99编译器吗?
是的,GCC,-std = c99。
谢谢,
Adam
-
Adam Richardson
Carpe Diem
Hi,
I''d like to return an (arbitrary length) string array from a function so
that after calling the array I''ve got a list of strings I can access.
After much perusing of the internet I found a related answer here (by
Eric Sosman) which involved creating an array of pointers and using
that, so it looks something like:
void foo(char *sptr[], int items)
{
char buff[50];
while (--items >= 0)
{
sprintf (buff, "foo%d", items);
sptr[items] = malloc(strlen(buff) + 1);
if (sptr[items] == NULL)
exit(0);
strcpy (sptr[items], buff);
}
}
int main(void)
{
int total = 10;
char *p[total];
foo(p, total);
printf(p[1]);
return 0;
}
This works a treat (I can access my strings, e.g. the printf line) but
how would I do a similar thing if I don''t know how long my list is going
to be? E.g. "total" only gets found out inside foo.
Thanks a lot,
Adam
--
Adam Richardson
Carpe Diem
"Adam" <ne**@snowstone.org.ukwrote in message
news:48**************@snowstone.org.uk...Hi,
I''d like to return an (arbitrary length) string array from a function so
that after calling the array I''ve got a list of strings I can access.
After much perusing of the internet I found a related answer here (by
Eric Sosman) which involved creating an array of pointers and using
that, so it looks something like:
void foo(char *sptr[], int items)
{
char buff[50];
while (--items >= 0)
{
sprintf (buff, "foo%d", items);
sptr[items] = malloc(strlen(buff) + 1);
if (sptr[items] == NULL)
exit(0);
strcpy (sptr[items], buff);
}
}
int main(void)
{
int total = 10;
char *p[total];
foo(p, total);
printf(p[1]);
return 0;
}
This works a treat (I can access my strings, e.g. the printf line) but
how would I do a similar thing if I don''t know how long my list is going
to be? E.g. "total" only gets found out inside foo.
Thanks a lot,
Adam
char **foo(int items)
{
char **answer;
char buff[100];
int i;
answer = malloc(items * sizeof(char *));
if(!answer)
goto error_exit;
/* needed for clean up, mustn''t have non-null garbage pointers */
for(i=0;i<items;i++)
answer[i] = 0;
for(i=0;i<items;i++)
{
sprintf(buff, "foo %d", i):
answer[i] = malloc(stren(buff) + 1);
if(!answer[i])
goto error_exit;
strcpy(answer[i], buff);
}
return answer;
error_exit:
if(answer)
for(i=0;i<items;i++)
free(answer[i]);
free(answer);
/* maybe print error message / terminate here */
return 0;
}
In article <Mt******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>"Adam" <ne**@snowstone.org.ukwrote in message
news:48**************@snowstone.org.uk...
>I''d like to return an (arbitrary length) string array from a function so
that after calling the array I''ve got a list of strings I can access.
answer = malloc(items * sizeof(char *));
if(!answer)
goto error_exit;
/* needed for clean up, mustn''t have non-null garbage pointers */Hmmm. Adam''s question didn''t -sound- like textbook homework, that you
needed to use a goto to down-grade the code quality so as to
down-grade the marks of anyone who copied it literally.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
In message <Wb******************************@comcast.com>, Eric Sosman
wrote:Adam wrote:I''d like to return an (arbitrary length) string array from a function so
that after calling the array I''ve got a list of strings I can access.
^^^^^ function
[snip discussion]
Thanks a lot for the helpful info. I''ll go away and do some pondering
about the best course of action. :)
Are you using a C99 compiler?Yep, GCC, -std=c99.
Thanks,
Adam
--
Adam Richardson
Carpe Diem
这篇关于从函数返回字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!