Andrew
Andrew

Reputation: 41

Pass by address of value in C

I know the following is an example of pass by reference in C++, input is passed as a reference:

void add(int &input){
 ++input;
}

I also know pass by reference is not available in C. My question is, does the above syntax mean something else in C (i.e pass by value or something), or is it meaningless? Trying to compile it in C gives this error:

error: parameter name omitted

Upvotes: 3

Views: 395

Answers (4)

dash-o
dash-o

Reputation: 14468

While C does not have pass by reference (and the code will produce compile error), you can get something closer by following the rules:

  • In the prototype, replace & with * const (reference cannot be reassigned).
  • In the body, replace reference to varname with (*varname)
  • When calling the method, replace arg with &(arg).
void add (int *const in)
{
   ++(*in) ;      // increment
   (*in) = 5 ;    // assign 
   int x = *in ;  // Copy value
}

Upvotes: 2

klutt
klutt

Reputation: 31409

does the above syntax mean something else in C?

No, it does not. It's not valid C at all.

The & operator means two things in C. The binary one is bitwise "and", and the unary is "address of". You cannot use it in declarations.

C++ chose this for reference variable for two reasons. The first is that since it is not valid C, it will not collide with existing C code. When C++ came, they focused pretty hard on making C++ backwards compatible with C. In later versions of C++, the backwards compability with C is not a very high priority. To a large degree, this is because C++ was a fork of a pretty early version of C, and since then both languages have evolved somewhat independently. For instance C99 added (but it was removed later) variable length arrays, which were never added to C++. Another example is designated initializers.

The other reason is that the meaning of the operator is pretty similar. You can interpret it as "instead of forcing the caller to send the address, I will take the address of whatever he is sending". They simply just moved the & to the function prototype instead of the function call.

And yes, there are a few other differences between pointers and references too

  • A reference must be initialized. (Assigned upon declaration)
  • A reference cannot be reassigned to "point" to another object.
  • A reference must always "point" at an object. It cannot be NULL.

There is one danger with references. In C, you can be certain that a function will never change the variables you send as arguments to a function unless you're sending the address to them. This C code:

int main(void)
{
    int a = 42;
    foo(a);
    printf("%d\n", a);
}

will ALWAYS print "42", no matter how the function foo is defined. Provided that the code compiles and there's no weird undefined behavior. In C++, you don't have that guarantee.

Upvotes: 7

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

No, it is simply invalid syntax in C.

That is actually one of the reasons that C++ picked this syntax for the feature: it wouldn't change the meaning of any existing C code.

Upvotes: 2

eerorika
eerorika

Reputation: 238401

does the above syntax mean something else in C (i.e pass by value or something), or it's meaningless?

It is meaningless. The program is syntactically ill-formed .

Upvotes: 1

Related Questions