从函数返回指针 [英] Return pointer from function

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

问题描述

说我有一个功能:


char * rmtrail(char * str)

{

if(str)

{

int i;


for(i = strlen(str) - 1; i> = 0& & isspace(str [i]); - i)

;

str [++ i] =''\ 0'';

}


返回str;

}


从字符串中删除尾随空格。这个

函数的结果会有所不同,如果我将返回类型从char *更改为

void并删除''return str''?我的意思是,在调用函数中,我调用

rmtrail,将指针传递给字符串。即使在rmtrail中,我还是收到了指向我的字符串的指针的副本,修改后的字符串将是/ b $ b是相同的,所以需要返回来自rmtrail的'str''

功能?这个功能没有由我编码,但它工作正常。

我只是想知道是否有任何区别......

解决方案

Liviu< ol **** @ gmail.comwrote:


假设我有一个功能:


char * rmtrail(char * str)

{

if(str)

{

int i;


for(i = strlen(str) - 1; i> = 0&& isspace(str [i]); - i)< br $>
;

str [++ i] =''\ 0'';

}


返回str;

}


从字符串中删除尾随空格。这个

函数的结果会有所不同,如果我将返回类型从char *更改为

void并删除''return str''?我的意思是,在调用函数中,我调用

rmtrail,将指针传递给字符串。即使在rmtrail中,我还是收到了指向我的字符串的指针的副本,修改后的字符串将是/ b $ b是相同的,所以需要返回来自rmtrail的'str''

功能?这个函数不是由我编写的,但它工作正常。

我只是想知道是否有任何区别...



区别在于你可以调用它的方式,但从功能上来说,它确实会保持不变。比较一下,比如strcpy();它也是,

返回一个指向你刚才复制的字符串的指针,这是你传入的指针。这就是你现在的方式。 strcpy()和

你的函数都可以返回任何内容并保持功能相同,

只消除一些不太可读的结构,例如

strcpy (rmtrail(strcat(rmtrail(......))));.

IMO,如果返回指针,两个函数都会得到改善,而不是
第一个,但结果的最后一个字符。这样,你可以至少使用结果来获得更高效的字符串

构造。


Richard


6月27日下午3:57,r ... @ hoekstra-uitgeverij.nl(Richard Bos)写道:


Liviu< oli ... @ gmail.comwrote:


说我有功能:


char * rmtrail(char * str)

{

if(str)

{

int i;


for(i = strlen(str) - 1; i> = 0&& isspace(str [i]); - i)

;

str [++ i] =''\ 0'';

}


returntr;

}


删除a的尾随空格串。这个

函数的结果会有所不同,如果我将其从char *更改为

void并删除''returnstr''?我的意思是,在callerfunctioni调用

rmtrail,传递apointerto字符串。即使在rmtrail中,我还是收到了我的字符串的副本,修改后的字符串将是/ b $ b是相同的,所以需要返回'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '来自rmtrail

功能?这个功能不是由我编写的,但它工作正常。

我只是想知道是否有任何区别...



区别将是你可以调用它的方式,但从功能上来说,它确实会保持不变。比较一下,比如strcpy();它也是,

返回你刚才复制的字符串,这是非常相同的信息。这就是你现在拥有它的方式。 strcpy()和

你的函数可以恢复并保持功能相同,

只消除一些不太可读的结构,例如

strcpy(rmtrail(strcat) (rmtrail(......))));

IMO,如果他们返回apointer,两个函数都会得到改善,而不是第一个返回
,但是结果的最后一个字符。这样,你可以至少使用结果来获得更高效的字符串

构造。


Richard



我现在明白了。所以,只要我不需要我的函数的结果

''rmtrail''作为其他函数的参数(使得一个不那么可读的

语法)我可以更改返回的类型(无效)没有任何不好

结果。

谢谢。


Liviu写道:


>

假设我有一个功能:


char * rmtrail(char * str){

if(str){

int i;

for(i = strlen(str) - 1; i> = 0&& isspace(str [i]); - i);

str [++ i] =''\ 0'';

}

返回str;

} / *为空间编辑,cbf * /



As写这个功能是一个使用陷阱。一个人可能会写下这样的话:


printf("%s \ n%s \ n",str,rmtrail(str) );


并期望得到两个不同的字符串。你不会。通过使

函数返回错误指示符或空白,您可以避免该陷阱。


-

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

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

< http://www.aaxnet.com/editor/edit043.html>

cbfalconer at maineline dot net


-

通过 http://www.teranews.com上的免费Usenet帐户发布


Say i have a function:

char *rmtrail(char *str)
{
if (str)
{
int i;

for (i = strlen(str) - 1; i >= 0 && isspace(str[i]); --i)
;
str[++i] = ''\0'';
}

return str;
}

that removes trailing spaces from a string. The result of this
function will be different, if I change the return type from char* to
void and remove ''return str'' ? I mean, in the caller function i call
rmtrail, passing a pointer to string. Even though in rmtrail, i
received a copy of the pointer to my string, the modified string will
be the same, so where''s the need to return ''str'' from rmtrail
function ? This function was not coded by me, but it works fine.
I just want to know if there is any difference...

解决方案

Liviu <ol****@gmail.comwrote:

Say i have a function:

char *rmtrail(char *str)
{
if (str)
{
int i;

for (i = strlen(str) - 1; i >= 0 && isspace(str[i]); --i)
;
str[++i] = ''\0'';
}

return str;
}

that removes trailing spaces from a string. The result of this
function will be different, if I change the return type from char* to
void and remove ''return str'' ? I mean, in the caller function i call
rmtrail, passing a pointer to string. Even though in rmtrail, i
received a copy of the pointer to my string, the modified string will
be the same, so where''s the need to return ''str'' from rmtrail
function ? This function was not coded by me, but it works fine.
I just want to know if there is any difference...

The difference will be in the way you can call it, but functionally, it
will indeed remain the same. Compare this with, say, strcpy(); it, too,
returns a pointer to the string you just copied, which is the very same
pointer you passed in. That''s the way you have it now. Both strcpy() and
your function could return nothing and remain functionally the same,
eliminating only some not very readable constructs such as
strcpy(rmtrail(strcat(rmtrail(......))));.
IMO, both functions would be improved if they returned a pointer, not to
the first, but to the last character of their results. That way, you
could at least use the result for slightly more efficient string
construction.

Richard


On Jun 27, 3:57 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:

Liviu <oli...@gmail.comwrote:

Say i have afunction:

char *rmtrail(char *str)
{
if (str)
{
int i;

for (i = strlen(str) - 1; i >= 0 && isspace(str[i]); --i)
;
str[++i] = ''\0'';
}

returnstr;
}

that removes trailing spaces from a string. The result of this
functionwill be different, if I change thereturntype from char* to
void and remove ''returnstr'' ? I mean, in the callerfunctioni call
rmtrail, passing apointerto string. Even though in rmtrail, i
received a copy of thepointerto my string, the modified string will
be the same, so where''s the need toreturn''str'' from rmtrail
function? Thisfunctionwas not coded by me, but it works fine.
I just want to know if there is any difference...


The difference will be in the way you can call it, but functionally, it
will indeed remain the same. Compare this with, say, strcpy(); it, too,
returns apointerto the string you just copied, which is the very samepointeryou passed in. That''s the way you have it now. Both strcpy() and
yourfunctioncouldreturnnothing and remain functionally the same,
eliminating only some not very readable constructs such as
strcpy(rmtrail(strcat(rmtrail(......))));.
IMO, both functions would be improved if they returned apointer, not to
the first, but to the last character of their results. That way, you
could at least use the result for slightly more efficient string
construction.

Richard

I understand now. So, as long as i dont need the result of my function
''rmtrail'' as a parameter to other function (making an not so readable
syntax) i can change the returned type (to void) without any bad
consequnces.
Thank you.


Liviu wrote:

>
Say i have a function:

char *rmtrail(char *str) {
if (str) {
int i;
for (i = strlen(str) - 1; i >= 0 && isspace(str[i]); --i) ;
str[++i] = ''\0'';
}
return str;
} /* edited for space, cbf */

As written that function is a usage trap. One is likely to write
something like:

printf("%s\n%s\n", str, rmtrail(str));

and expect to get two different strings. You won''t. By making the
function return an error indicator, or a void, you avoid that trap.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于从函数返回指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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