C and C++ Pass by Pointer and Pass by Reference Confusion

So I understand how to pass in addresses to a C function to avoid passing in the value. But what I do not understand is passing in by reference in C++. My lack of understanding comes from C where, if you were to pass in the variables by reference, you would just get a nonsense int value based on the address. Such as if you did:

int x = 5;
int* p = x;
int y = p;

The y would be a nonsense int value. So I do not understand C++ pass by reference based on the above logic. Is there some compiler identification of what you are trying to do so it compiles it in a way that makes it equivalent to the C code? See below for C and C++ pass by pointer and by reference:

// C pass by pointer value
void main() {
    int a = 1;
    int b = 2;

    swap(&a, &b);
    }

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// C++ pass by reference
void main() {
    int a = 1;
    int b = 2;

    swap(a, b);
    }

void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}
5

1 Answer

You are asking a question that starts holy wars.

Objects

An object is an instance of some kind of thing: it has its own memory and everything. A lot of objects in our code get names.

int number_of_birds_in_hand = 0;
std::string users_name = "Fred";

You see the pattern: type, name, value.

The name is a direct reference to the object, or alias.

Pointers

Pointers are indirect references to an object. They are indirect because they are themselves objects, having names and values all their own:

const char * filename = "hello.txt";

We tend to confuse it because it looks like filename has the value "hello.txt", but that is not the case. filename’s value is the address of the text. We can change that address any time:

filename = "goodbye.txt";

(Like any variable in C and C++ we can tell the compiler not to let us do that, with: const char * const filename.)

We dereference a pointer to get a direct reference to the object. The direct reference has no name, but it resolves to the same as having that object’s name.

int x = 12;
int * y = &x;
*y = -7;

The construct *y resolves to a direct reference (or alias) to x: accessing *y is the same as accessing x. But y itself is only a reference to its own object value.

References

C++ adds a new construct, called a reference. From the language point of view, that is exactly what it is: another name for an object.

int x = 12;
int & y = x;
y = -7;

Here, both x and y are aliases (or direct references) to the same object!

Holy Wars

The way we speak about these things gets people going. The C++ Standard has one way of doing it. CS literature throughout time has another. Very intelligent, knowledgeable people can vary in their opinions on this topic.

I personally have no problem referring to a pointer as a reference when talking in C. But in C++ such language gets confused with true references (not the indirect references that pointers are).

A common argument people like to have is whether references are really pointers or not. The answer is that it depends. Sometimes they are, sometimes they aren’t.

It is an infantile argument, because from the point of view provided by the C++ language they are different things. How they work underneath really only matters to the people who make compilers.

And then you have the whole pass-by-pointer holy war too... but best leave that one for a different SO question.

Final Thoughts

The trick is to understand what people mean when they refer to these things, and if you are unsure just ask. If someone gets loud and angry about the way you referred to it, just try to use the terminology that they used. They can go have baby cows without you.

In common C++, you have object values, pointers, and references, and depending on whom you ask, never shall they be confused. If you are unsure, just say that you passed a pointer to a function so that you could dereference it to get the target value, heh.

5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest