在C中返回一个字符串数组 [英] Returning an array of strings in C

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

问题描述




我正在编写一个需要返回一个字符串数组的函数,而且我很难解决它的问题。我需要一些帮助。


这是我认为的100个字符串的数组:

char * array_string [100]


谢谢,

-Kim

Hi,

I am writing a function that needs to return an array of strings and I
am having some trouble getting it right. I need some help.

Here is what I consider an array of 100 strings:
char *array_string[100]

Thanks,
-Kim

推荐答案

您的返回类型是char **,然后在你的功能中使用:


功能名称(args ......)

{

...

返回array_string;

}


2月20日上午9:52,klear ... @ gmail.com写道:
Your return type is char**, then in your function use:

function name (args ...)
{
...
return array_string;
}

On Feb 20, 9:52 am, klear...@gmail.com wrote:




我正在编写一个需要返回一个字符串数组的函数,我

am在纠正它时遇到一些麻烦。我需要一些帮助。


这是我认为的100个字符串的数组:

char * array_string [100]


谢谢,

-Kim
Hi,

I am writing a function that needs to return an array of strings and I
am having some trouble getting it right. I need some help.

Here is what I consider an array of 100 strings:
char *array_string[100]

Thanks,
-Kim



kl ****** @ gmail.com 写道:

>

我正在编写一个函数,需要返回一个字符串数组

而且我遇到了一些麻烦。我需要一些帮助。


这是我认为的100个字符串数组:

char * array_string [100]
>
I am writing a function that needs to return an array of strings
and I am having some trouble getting it right. I need some help.

Here is what I consider an array of 100 strings:
char *array_string[100]



#include< stdio.h>

#include" ggets.h"

#define MAXSTR 100


int main(无效)

{

char * array_strp [MAXSTR + 1]; / * +1允许终端NULL * /

char * bfr;

char ** dumper;

int i;


i = 0;

while((i< MAXSTR)&&(0 == ggets(& bfr))){

array_strp [i ++] = bfr;

}

array_strp [i] = NULL;


/ * dump'' em all * /

dumper =& array_strp [0];

while(* dumper)puts(* dumper ++);

返回0 ;

} / *已测试* /


将从stdin读取的行中填充字符串数组。你可以在
找到ggets:


< http://cbfalconer.home.att.net/download/>


(您也可以使用同一个包中的fggets从任意

任意文件中读取)


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

< http://www.securityfocus.com/columnists/ 423>


每次都是正确的男人不太可能做得太多。

- Francis Crick,共同发现DNA

没有什么比行动中的愚蠢更令人惊讶了。

- Thomas Matthews

#include <stdio.h>
#include "ggets.h"
#define MAXSTR 100

int main(void)
{
char *array_strp[MAXSTR+1]; /* +1 allows for terminal NULL */
char *bfr;
char **dumper;
int i;

i = 0;
while ((i < MAXSTR) && (0 == ggets(&bfr))) {
array_strp[i++] = bfr;
}
array_strp[i] = NULL;

/* dump ''em all */
dumper = &array_strp[0];
while (*dumper) puts(*dumper++);
return 0;
} /* tested */

will fill the string array from lines read from stdin. You can
find ggets at:

<http://cbfalconer.home.att.net/download/>

(You can also use fggets from the same package to read from any
arbitrary file)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

kl******@gmail.com 说:




我正在编写一个需要返回一个字符串数组的函数,而且我需要一些东西才能获得它对。我需要一些帮助。


这是我认为的100个字符串数组:

char * array_string [100]
Hi,

I am writing a function that needs to return an array of strings and I
am having some trouble getting it right. I need some help.

Here is what I consider an array of 100 strings:
char *array_string[100]



这里有三个问题:


1)你到目前为止收到的答案很差,

其中一个给了你错误信息,其中第二个完全忽略了你的问题,这似乎是一个相当奇怪的尝试

" push"你身上有一个非标准的功能(你在玩什么,Chuck?);


2)你认为100个字符串的数组实际上并不是一个
100个字符串,但是指向char的100个指针的数组 - 但是,它可以是用于存储100个指针的指针,这些字符是
$的第一个字符。 b $ b字符串,所以这不是一个糟糕的起点,但你必须考虑

内存问题(见下文);


3)在C语言中,没有从函数返回数组的语法。


您可以改为:


a)为数组创建内存在函数本身内,并且
返回指向该数组的第一个元素的地址的指针;

b)通过参数将数组的内存传递给函数;

c)完全绕过模块化原则。


c)如果还有其他方式永远不会好,所以你最好的选择是
a)和b)之间的



您还需要明确字符串的来源,以及是否只需要指向它们或制作它们的实际副本。在设置示例时,它会产生

a的差异。


-

Richard Heathfield

" ; Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

You have three problems here:

1) you have been poorly-served by the answers you''ve received so far,
one of which gave you misinformation and the second of which ignored
your question completely in what seems a rather strange attempt to
"push" a non-standard function onto you (what ya playin'' at, Chuck?);

2) what you consider an array of 100 strings is not in fact an array of
100 strings, but an array of 100 pointers to char - it can, however, be
used for storing 100 pointers to chars that are the first characters of
strings, so it''s not a bad starting place, but you have to consider
memory issues (see below);

3) in C, there is no syntax for returning an array from a function.

You could instead:

a) create the memory for the array within the function itself, and
return a pointer to the address of that array''s first element;
b) pass the memory for the array into the function via a parameter;
c) bypass the modularity principle completely.

c) is never good if there''s some other way, so your best choice is
between a) and b).

You also need to make it clear where the strings come from, and whether
you just want to point to them or make actual copies of them. It makes
a difference when setting up an example.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


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

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