返回字符串或指针的函数 [英] A function returning string or pointer
问题描述
大家好,
来自我之前的帖子我很忙将代码分成
函数。
我遇到的一个问题(中有很多)是如何正确使用/ b $ b函数返回一个字符数组(字符串)或
a指针这个数组。
我读了一些文章,其他一些帖子并找到了这个解决方案:
char * read_name(void){
static char item_name [11];
char * p_item_name;
printf("输入描述:");
if(fgets(item_name,sizeof(item_name),stdin)!= NULL){
/ *如果输入包含新行* /
if( (p_item_name = strchr(item_name,''\ n''))!= NULL){
* p_item_name =''\'''; / *摆脱新行* /
}
else {
while(getchar()!=''\ n''' ){/ *摆脱缓冲区中的剩余部分* /
;
}
}
}
return item_name; / *以字符数组的形式返回字符串* /
}
还有其他解决方案吗?使用
数组/指针有什么利弊?
svata
Hello to all,
as a result from my previous post I''m busy with splitting code into
functions.
The one problem ( out of many ) I encounter is how to properly
use/code a function which returns either array of characters(string) or
a pointer to this array.
I read some articles, some other posts and come to this solution:
char *read_name(void){
static char item_name[11];
char *p_item_name;
printf("Enter the description: ");
if (fgets(item_name, sizeof(item_name), stdin) != NULL){
/* if the input contains a new line */
if (( p_item_name = strchr(item_name, ''\n'')) != NULL ){
*p_item_name = ''\0''; /* get rid of new line */
}
else {
while(getchar() != ''\n''){ /* get rid of the rest in the buffer */
;
}
}
}
return item_name; /* return string in the form of character array */
}
Are there other solutions? What are the pros and cons using
array/pointer?
svata
推荐答案
你好Svata,
Hello Svata,
因为我以前的帖子我忙着将代码分成
函数。
我遇到的一个问题(中有很多)是如何正确使用/ b $ b函数返回一个字符数组(字符串)或
a指向此数组的指针。
我读了一些文章,其他一些帖子并且来到这个解决方案:
as a result from my previous post I''m busy with splitting code into
functions.
The one problem ( out of many ) I encounter is how to properly
use/code a function which returns either array of characters(string) or
a pointer to this array.
I read some articles, some other posts and come to this solution:
< snip>
<snip>
还有其他解决方案吗?使用
数组/指针有什么优缺点?
Are there other solutions? What are the pros and cons using
array/pointer?
您的代码没问题,特别是如果item_name读取应该被截断
到10个字符(此限制可以定义为常量,BTW ,使
代码变更容易)。
干杯,
Loic。
Your code is fine, especially if the item_name read should be truncated
to 10 characters (this limit could be defined as constant, BTW, to make
code change easier).
Cheers,
Loic.
svata写道:
svata wrote:
大家好,
来自我以前的结果发布我很忙将代码拆分成
函数。
我遇到的一个问题(中有很多)是如何正确的
使用/编写一个函数,它返回指向此数组的字符数组(字符串)或
a指针。
Hello to all,
as a result from my previous post I''m busy with splitting code into
functions.
The one problem ( out of many ) I encounter is how to properly
use/code a function which returns either array of characters(string) or
a pointer to this array.
你/不得/返回指向数组的指针,该数组是函数的本地
非静态变量。
You /must not/ return a pointer to an array which is a local
non-static variable of the function.
我读了一些文章,其他一些帖子并找到了这个解决方案:
char * read_name(void){
static char item_name [11];
I read some articles, some other posts and come to this solution:
char *read_name(void){
static char item_name[11];
(fx:snip)
(fx:snip)
return item_name; / *以字符数组的形式返回字符串* /
}
return item_name; /* return string in the form of character array */
}
所以你/不能/这样做。当函数返回时,变量`item_name`会消失,因此指向它的指针不会指向任何地方
并且任何使用它都会使你获得未定义的行为 - 其中是一个很好的
Bad Thing。
你必须返回一个指向商店的指针,这个指针会比这个函数更长时间
来电:例如:
*一个静态数组,通常是一个坏主意,因为
函数的不同用途将共享该数组。
* mallocated商店(使用代码必须免费)
*商店作为参数传入(所以这是调用者的问题)
*根据需要混合(仔细)
-
Chris" hantwig efferko VOOM!" Dollin
含义在定义之前。
So you /must not/ do this. The variable `item_name` evaporates when
the function returns, so the pointer to it isn''t pointing anywhere
and any use of it gets you undefined behaviour -- which is a Very
Bad Thing.
You must return a pointer to store which will outlive the function
call: for example:
* a static array, usually a bad idea because different uses of the
function will share that array.
* mallocated store (which the using code will have to free)
* store passed in as an argument (so it''s the caller''s problem)
* mix as desired (carefully)
--
Chris "hantwig efferko VOOM!" Dollin
Meaning precedes definition.
lo ****** @ gmx.net 写道:
lo******@gmx.net wrote:
Hello Svata,
Hello Svata,
>因为我以前的帖子我忙着把代码分成
函数。
我遇到的一个问题(很多)是如何正确的
使用/编写一个函数,它返回一个字符数组(字符串)或
一个指向这个数组的指针。
我读了一些文章,其他一些文章并提出了这个解决方案:
>as a result from my previous post I''m busy with splitting code into
functions.
The one problem ( out of many ) I encounter is how to properly
use/code a function which returns either array of characters(string) or
a pointer to this array.
I read some articles, some other posts and come to this solution:
< snip>
<snip>
>还有其他解决方案吗?使用
数组/指针有什么优缺点?
>Are there other solutions? What are the pros and cons using
array/pointer?
您的代码没问题,
Your code is fine,
不是这样,正如我对svata的回复所解释的那样。
-
Chris" hantwig efferko VOOM!" Dollin
人们是设计的一部分。忘记这一点是危险的。 / Star Cops /
Not so, as my reply to svata explains.
--
Chris "hantwig efferko VOOM!" Dollin
"People are part of the design. It''s dangerous to forget that." /Star Cops/
这篇关于返回字符串或指针的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!