开发者

Is it possible to refer to the function return value in C?

Is it possible to somehow refer to开发者_JAVA技巧 the value I am returning from a function? An example explains better:

CFTypeRef foo()
{
    CFTypeRef valueRef = NULL;
    bar(&valueRef); // fills valueRef with some data
    return valueRef;
}

I thought it would be nice to rewrite this as:

CFTypeRef foo()
{
    bar(&__retvalue);
}

Where of course __retvalue would be some magical token. Does this make sense? Is it possible to do that? If not, why?


This is not possible. At a low level, the return value is usually returned in a processor register, making it impossible to pass as a reference.


a) It makes sense. b) There is no such magic token. c) The "If not, why"? question is just bizarre ... Why isn't there such a magic token? Because the language designers never thought of it or, thinking of it, didn't think it was a good thing to add to the language. (Someone mentioned that the return value is usually held in a register but that's irrelevant; the compiler could generate code to load that register from an in-memory variable, exactly as happens in your current foo).


Probably not.

Maybe you could define a macro if you're looking to make the code cleaner?


You could make bar() return its argument. You would save a line of code, if that's the point.

CFTypeRef foo()
{
    CFTypeRef valueRef = NULL;
    return bar(&valueRef);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜