本文介绍了从函数返回指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 说我有一个功能: 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'' 功能?这个功能没有由我编码,但它工作正常。 我只是想知道是否有任何区别...... 解决方案 区别在于你可以调用它的方式,但从功能上来说,它确实会保持不变。比较一下,比如strcpy();它也是, 返回一个指向你刚才复制的字符串的指针,这是你传入的指针。这就是你现在的方式。 strcpy()和 你的函数都可以返回任何内容并保持功能相同, 只消除一些不太可读的结构,例如 strcpy (rmtrail(strcat(rmtrail(......))));. IMO,如果返回指针,两个函数都会得到改善,而不是第一个,但结果的最后一个字符。这样,你可以至少使用结果来获得更高效的字符串 构造。 Richard 区别将是你可以调用它的方式,但从功能上来说,它确实会保持不变。比较一下,比如strcpy();它也是, 返回你刚才复制的字符串,这是非常相同的信息。这就是你现在拥有它的方式。 strcpy()和 你的函数可以恢复并保持功能相同, 只消除一些不太可读的结构,例如 strcpy(rmtrail(strcat) (rmtrail(......)))); IMO,如果他们返回apointer,两个函数都会得到改善,而不是第一个返回,但是结果的最后一个字符。这样,你可以至少使用结果来获得更高效的字符串 构造。 Richard 我现在明白了。所以,只要我不需要我的函数的结果 ''rmtrail''作为其他函数的参数(使得一个不那么可读的 语法)我可以更改返回的类型(无效)没有任何不好 结果。 谢谢。 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 thisfunction will be different, if I change the return type from char* tovoid and remove ''return str'' ? I mean, in the caller function i callrmtrail, passing a pointer to string. Even though in rmtrail, ireceived a copy of the pointer to my string, the modified string willbe the same, so where''s the need to return ''str'' from rmtrailfunction ? 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, itwill indeed remain the same. Compare this with, say, strcpy(); it, too,returns a pointer to the string you just copied, which is the very samepointer you passed in. That''s the way you have it now. Both strcpy() andyour function could return nothing and remain functionally the same,eliminating only some not very readable constructs such asstrcpy(rmtrail(strcat(rmtrail(......))));.IMO, both functions would be improved if they returned a pointer, not tothe first, but to the last character of their results. That way, youcould at least use the result for slightly more efficient stringconstruction.RichardThe difference will be in the way you can call it, but functionally, itwill 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() andyourfunctioncouldreturnnothing and remain functionally the same,eliminating only some not very readable constructs such asstrcpy(rmtrail(strcat(rmtrail(......))));.IMO, both functions would be improved if they returned apointer, not tothe first, but to the last character of their results. That way, youcould at least use the result for slightly more efficient stringconstruction.RichardI 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 readablesyntax) i can change the returned type (to void) without any badconsequnces.Thank you.As written that function is a usage trap. One is likely to writesomething like:printf("%s\n%s\n", str, rmtrail(str));and expect to get two different strings. You won''t. By making thefunction 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 这篇关于从函数返回指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 01:43
查看更多