0
votes

I know that C uses pass-by-value and we can emulate pass-by-reference with the help of pointers. But, for example, in order to calculate a simple mathematical expression, how do I implement pass-by-name (which is kind of lazy evaluation but not exactly) in C?

2
Please explain what do you mean by pass by name, and how is that different from pass by value, and pass by referenceRishikesh Raje
@RishikeshRaje Well, rather not. search here.Sourav Ghosh
Never heard of it myself, but LMGTFY gave me: stackoverflow.com/questions/838079/…Lundin
@Olaf , what I mean is emulating.phoeNix
It might be possible to implement this using the gcc extension which supports nested functions, which could then be passed as arguments. The caller would then call the functions when it needs the arguments. But this is not portable and is not true C, so it would only work with gcc with the extension enabled. I suppose you could instead use structures that contain both data pointers and function pointers. It's ugly no matter how you do it.Tom Karzes

2 Answers

3
votes

C is only pass-by-value. You can't pass by reference or name. With the pre-processor you can do various hacks but not in the C language.

Sometimes, people call passing a pointer "pass-by-reference" but this is not the case. The pointer is passed by value like anything else. C++ is a different story but you asked about C.

You might also be interested in this article discussion this at length

0
votes

The parameter substitution used by function-like preprocessor macros is sometimes described as being "pass by name".