Are pointers considered a method of calling by reference in C?

In my University's C programming class, the professor and subsequent book written by her uses the term call or pass by reference when referring to pointers in C.

An example of what is considered a 'call by reference function' by my professor:

int sum(int *a, int *b);

An example of what is considered a 'call by value function' by my professor:

int sum(int a, int b);

I've read C doesn't support call by reference. To my understanding, pointers pass by value.

Basically, is it incorrect to say pointers are C's way of passing by reference? Would it be more correct to say you cannot pass by reference in C but can use pointers as an alternative?


Update 11/11/15

From the way my question originated, I believe a debate of terminology has stemmed and in fact I'm seeing two specific distinctions.

  • pass-by-reference (the term used mainly today): The specific term as used in languages like C++
  • pass-by-reference (the term used by my professor as a paradigm to explain pointers): The general term used before languages like C++ were developed and thus before the term was rewritten

After reading @Haris' updated answer it makes sense why this isn't so black and white.


Solution 1:

you cannot pass by reference in C but can use pointers as an alternative

Yup, thats correct.


To elaborate a little more. Whatever you pass as an argument to c functions, it is passed by values only. Whether it be a variable's value or the variable address.

What makes the difference is what you are sending.

When we pass-by-value we are passing the value of the variable to a function. When we pass-by-reference we are passing an alias of the variable to a function. C can pass a pointer into a function but that is still pass-by-value. It is copying the value of the pointer, the address, into the function.


  • If you are sending the value of a variable, then only the value will be received by the function, and changing that won't effect the original value.

  • If you are sending the address of a variable, then also only the value(the address in this case) is sent, but since you have the address of a variable it can be used to change the original value.


As an example, we can see some C++ code to understand the real difference between call-by-value and call-by-reference. Taken from this website.

// Program to sort two numbers using call by reference. 
// Smallest number is output first.

#include <iostream>
using namespace std;

// Function prototype for call by reference
void swap(float &x, float &y);

int main()
{
   float a, b;

   cout << "Enter 2 numbers: " << endl;
   cin >> a >> b;
   if(a>b) 
     swap(a,b); // This looks just like a call-by-value, but in fact
                // it's a call by reference (because of the "&" in the
                // function prototype

   // Variable a contains value of smallest number
   cout << "Sorted numbers: ";
   cout << a << " " << b << endl;
   return 0;
}

// A function definition for call by reference
// The variables x and y will have their values changed.

void swap(float &x, float &y)
// Swaps x and y data of calling function
{
   float temp;

   temp = x;
   x = y;
   y = temp;
}

In this C++ example, reference variable(which is not present in C) is being used. To quote this website,

"A reference is an alias, or an alternate name to an existing variable...",

and

"The main use of references is acting as function formal parameters to support pass-by-reference..."

This is different then the use of pointers as function parameters because,

"A pointer variable (or pointer in short) is basically the same as the other variables, which can store a piece of data. Unlike normal variable which stores a value (such as an int, a double, a char), a pointer stores a memory address."

So, essentially when one is sending address and receiving through pointers, one is sending the value only, but when one is sending/receiving a reference variable, one is sending an alias, or a reference.


**UPDATE : 11 November, 2015**

There has been a long debate in the C Chatroom, and after reading comments and answers to this question, i have realized that there can be another way to look at this question, another perspective that is.

Lets look at some simple C code

int i;
int *p = &i;
*p = 123;

In this scenario, one can use the terminology that, p's value is a reference to i. So, if that is the case, then if we send the same pointer (int* p) to a function, one can argue that, since i's reference is sent to the function, and thus this can be called pass-by-reference.

So, its a matter of terminology and way of looking at the scenario.

I would not completely disagree with that argument. But for a person who completely follows the book and rules, this would be wrong.


NOTE: Update inspired by this chat.

Solution 2:

Reference is an overloaded term here; in general, a reference is simply a way to refer to something. A pointer refers to the object pointed to, and passing (by value) a pointer to an object is the standard way to pass by reference in C.

C++ introduced reference types as a better way to express references, and introduces an ambiguity into technical English, since we may now use the term "pass by reference" to refer to using reference types to pass an object by reference.

In a C++ context, the former use is, IMO, deprecated. However, I believe the former use is common in other contexts (e.g. pure C) where there is no ambiguity.

Solution 3:

Does C even have ``pass by reference''?

Not really.

Strictly speaking, C always uses pass by value. You can simulate pass by reference yourself, by defining functions which accept pointers and then using the & operator when calling, and the compiler will essentially simulate it for you when you pass an array to a function (by passing a pointer instead, see question 6.4 et al.).

Another way of looking at it is that if an parameter has type, say, int * then an integer is being passed by reference and a pointer to an integer is being passed by value.

Fundamentally, C has nothing truly equivalent to formal pass by reference or c++ reference parameters.

To demonstrate that pointers are passed by value, let's consider an example of number swapping using pointers.

int main(void)
{
    int num1 = 5;
    int num2 = 10;

    int *pnum1 = &num1;
    int *pnum2 = &num2;
    int ptemp;

    printf("Before swap, *Pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
    temp = pnum1;
    pnum1 = pnum2;
    pnum2 = ptemp;

    printf("After swap, *Pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
}

Instead of swapping numbers pointers are swapped. Now make a function for the same

void swap(int *pnum1, int *pnum2)
{
     int *ptemp = pnum1;
     pnum1 = pnum2;
     pnum2 = temp;
}

int main(void)
{
    int num1 = 5;
    int num2 = 10;

    int *pnum1 = &num1;
    int *pnum2 = &num2;

    printf("Before swap, *pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
    swap(pnum1, pnum2);

    printf("After swap, *pnum1 = %d and *pnum2 = %d\n", *pnum1, *pnum2);
}

Boom! No swapping!


Some tutorials mention pointer reference as call by reference which is misleading. See the this answer for the difference between passing by reference and passing by value.