开发者

C (not C++) pass by value/reference interview question [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I've got a list of questions I like to ask to probe a candidates C skills for embedded systems programming. One of the standard questions I have, which is also on a lot of public question lists is this but I'm starting to think it's a bad question:

"Explain or contrast pass-by-value and pass-by-reference in C"

I tell the interviewee that the question has nothing to do with C++ references and I'm looking for an explanation of passing a pointer to a variable vs. a variable and how the callee can modify the variable referenced by the pointer. Extra points for explanation about how passing pointers to structs is more efficient.

Here's the question: is there really "pass by reference" in C?

The signature of the callee clearly defines what's being passed. It's not like you can pass either x or &x to a function and have the compiler figure out what to do. So I would argue that everything is C is pass by value and pass-by-reference is really just passing the value of a pointer.

Is there something fundamentally different about

void f(int *xarg);
...
int x;
int *xp = &x;
f开发者_如何学Go(xp);

vs.

f(&x);

Thanks, Andrew


There is no pass by reference in C.

The closest you can get is pass by pointer, which is actually a pass by value (except that it is the pointer that is copied, not the pointed-to object).

So, you are correct.


No - there is no pass by reference in C - everything is pass by value. But I think it would be a bit hard on an interviewee if they explained pass by reference in terms of pointers and you rejected them because of that, unless you are looking for a language lawyer.


C does not have pass-by-reference.

Why not refer to it as "pass-by-pointer" instead?


C is always pass by value. There really isn't any difference between the code snippets you've posted. In fact an optimizing compiler would probably emit the same code either way.


Of course C has "pass by reference"... it's a well established Comp. Sci. term that's widely used to refer to the practice of providing access to a variable from the calling context. Crucially, it's the pointed-to variable that's passed by reference. The pointer to it that is being passed by value is just the mechanism for this that's available in C.

Any good C programmer should know exactly what you mean by your original question and not get it confused with "references" as per C++ etc..

From the draft C99 Standard:

A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’.

(That said, your insight that pointers are passed as values is an important part of understanding the hands-on way the language works, and valuable in its own right.)

(Meta-discussion - I rather think there's been one well-respected S.O. contributor take an extreme view on this and others are jumping on the bandwagon... the conformity in current "C lacks pass-by-reference" answers is definitely not representative of industry-accepted terminology. Not pointing fingers there - I haven't even tried to find out who responded first. As in judging art for competitions, published critiques etc. - you can't do it fairly and responsibly unless you form your own opinion before considering others. That said, another plausible explanation is that the logic in the question itself softened everyone up :-) )

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜