Passing value by reference in C and C++

Passing a reference in C:

1
2
3
4
5
6
7
8
9
10
11
12
void foo(int * a)
{
 ...
 *a=1;
 ...
}

int main()
{
 int a;
 foo(&a);
}


Passing a reference in C++

1
2
3
4
5
6
7
8
9
10
11
12
void foo(int & a)
{
 ...
 a=1;
 ...
}

int main()
{
 int a;
 foo(a);
}


My question is, why have the C standards committee not implemented this neater C++ syntax into their latest revions most notably the C99 revision?
Last edited on
closed account (1vRz3TCk)
C does not pass by reference.

From Wikipedia:
Function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.
http://en.wikipedia.org/wiki/C_(programming_language)
The real question is why should they? They didn't implement classes or templates, either. =P

References are a C++ thing. They don't really belong in C.
The real question is why should they? They didn't implement classes or templates, either. =P


Why should they? Because its syntactically cleaner. Why settle for typing more shit when you can simplify it.

Its not a fundamental change so I do not see the strawman argument of why not implement classes and templates while we are at it.

Changing it to C++ syntax lets the compiler "do the work for you" so you are not really changing the language itself that much.

C++ has its built in "& argument" feature to implement reference parameters for the programmer. The short story is, append an '&' to the type of a parameter, and the compiler will automatically make the parameter operate by reference for you. The type of the argument is not disturbed by this — the types continue to act as they appear in the source, which is the most convenient for the programmer.
Last edited on
closed account (1vRz3TCk)
but the is no concept of a reference type in C, you would have to make large changes to the language.
From what I understand and read there is no concept of a reference type in C++ aswell. In both languages you pass the pointers to the called function, difference being that in C++ the compiler automatically hides the complete pointer definition details away from you. This is from what I read.
Last edited on
Because its syntactically cleaner.


That's subjective. One could argue that references add ambiguity to syntax since the syntax for interacting with them is the same as intereacting with normal variables. More ambiguity != cleaner.

(not that I agree with that statement, though I certainly understand that viewpoint)

Its not a fundamental change


It really is. It's adding a whole new kind of variable. That's a very significant change.



But really.. the bottom line is... C is not C++. If you like C++ features (like references), then use C++.

If you don't like that C doesn't have those features, then don't use C.
1
2
3
4
5
6
7
8
9
10
11
12
void foo(int & a)
{
 ...
 a=1;
 ...
}

int main()
{
 int a;
 foo(a);
}


Compiler detects called function accepts a pointer. Substitutes in it int *a
Compiler detects that same variable a and replaces it with *a.
Finally the compiler detects caller funcion and substitus &a instead of a.

I personally see that as the only change that needs to be made. If there are very deep fundamental changes to be made, then the simple question is: what are they? And until you present to me these underlying changes, you, sir, will have to clarify me on this:

You mean to tell me if Ritchie was stoned in '69 and made his assignment operator to be =**= and C++ revised it to =, you would be sitting here telling me, "well if you dont like C, use C++?"
Last edited on
closed account (1vRz3TCk)
Some problems do arise from the multiple uses of the same terms in Computer Science and languages. For instance a pointer is a reference to an object but it is not the same as a reference in C++. The when you talk about pass by reference, you are talking about passing a C++ reference into a function.

How about: http://en.wikipedia.org/wiki/Reference_(C%2B%2B)


Edit:
C has pointer types.
C++ has pointer and reference types (they are not the same).
Last edited on
Even considering these underlying differences, a compiler can still substitute the necessary operators to seem like C++ code.
I guess I still don't see the point.

You're basically saying "why doesn't C implement X feature that I like?".

I look at that question from the other perspective: "why should C implement X feature?".

This hasn't really been answered. All you said was "it's cleaner", but that's not really true. It adds a level of ambiguity:

1
2
3
4
5
int foo = 0;
func(foo);  // does this modify foo?

// in C:  no
// in C++, maybe.  it's ambiguous.  You'd need to see func's prototype 


In C++, with the addition of things like classes, templates, operator overloading, etc, the use of references has additional benefits when coupled with those other features. So those benefits outweigh the ambiguities.

In C it just doesn't make sense, IMO.
closed account (1vRz3TCk)
the other thing to realise is that C99 has nothing to do with C++03 (and I doubt C++11). How C develops is not influenced by C++ development, they only have a common ancestor.
Last edited on
My question is, why have the C standards committee not implemented this neater C++ syntax into their latest revions most notably the C99 revision?


etc, etc...

Why? Because if they implement references, and classes, and templates, and, and, and... you end up with C being C++.
Why settle for typing more shit when you can simplify it.

If you're coding in pure C, then you're obviously a masochist. C programmers welcome, if not love typing more than necessary. Adding a pass by reference feature to C would infuriate those programmers to the point of forcing them to migrate to assembler in protest.
Topic archived. No new replies allowed.