1

I know how to pass the pointer in function, I feel that I still confuse with two terminologies, such as pass by reference and pass by pointer.

I was asked this:

Write a program that uses pointers to pass a variable x by reference to a function where its value is doubled. The return type of the function should be void. The value of the variable should be printed from main() after the function call. The original value of x is 5.

I wrote the code below. can you tell me which comment number is right according to the question's demand and clear my doubt?

#include <stdio.h>
//double pointer
//void double_ptr(int **x_ptr)
//{
 //   **x_ptr *=2;

//}

void double_ptr(int *x_ptr)
{
    *x_ptr *=2;
}


int main()
{
    int x=5;
    int *x_ptr=&x;      // single pointer

    //double_ptr(*x_ptr);  // this is out of question's demand.
    //int**x_x_ptr=&x_ptr;    // double pointer
    //double_ptr(&x_ptr);     // 1. pass with double pointer
    //double_ptr(x_x_ptr);      //2. pass with double pointer
    //printf("%d",**x_x_ptr); //3.print with double pointer

    double_ptr(x_ptr);     //4.pass by pointer?
    //double_ptr(&x);          //5.pass by reference?
    printf("%d",*x_ptr);
    return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Rishav
  • 53
  • 1
  • 9
  • 4
    Pure C doesn't support "pass by reference". You are either passing a value (`f(x)`), or passing the value of the pointer to a value (directly via `f(&x)` or via `int *xp = &x; f(xp);`). So if you're asked to give an example of passing by reference, the terminology is being used very loosely and may simply mean the `f(&x)` case. – lurker Dec 07 '13 at 16:34
  • 3
    To clarify, C doesn't have references, so when someone says "pass by reference" _in the C language_ they really mean "pass by pointer" – Mooing Duck Dec 07 '13 at 16:38
  • see here for a nice tutorial http://www.codingunit.com/c-tutorial-call-by-value-or-call-by-reference – holzben Dec 07 '13 at 16:50
  • Everyone honest is confused since addresses have been declared values by some language designers living on an island. From there comes the distraction, that an instance referenced by a pointer should be called "by-value". Before this, terms were straight. https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference – Sam Ginrich Feb 25 '22 at 09:11
  • @lurker do not agree. https://stackoverflow.com/questions/2229498/passing-by-reference-in-c – Sam Ginrich Feb 25 '22 at 09:15
  • @SamGinrich the accepted answer to this question as well as the one you linked agree with what I said in my comment: standard C does not support pass by reference. It's pass by pointer or by value. Sometimes I've seen the pointer passing referred to as a "reference" but that's not the same thing. – lurker Feb 26 '22 at 03:29
  • @lurker I'm aware that people draw back to the technical layer, where there is a pointer on a stack, but that's only half of the answer I referenced. In context of that question the answer states "Yes [calling by reference is possible]" and "it is done by passing the pointer to the value". So I don't agree with your conclusion, that passing a pointer excludes the "pass by reference" semantics, actually *x_ptr above (embedding C into C++ terminology) **is** a reference. You can't avoid passing the reference, when you pass the pointer of some [in/out] value. – Sam Ginrich Feb 26 '22 at 22:43
  • @SamGinrich maybe it's semantics, but whether it's a reference or a pointer is mostly about how the language manages the type at programmer's level. C doesn't have references as a type available to the programmer, with the specific behaviors, in the same sense that, for example, C++ does. – lurker Feb 27 '22 at 17:22
  • 1
    @SamGinrich perhaps consider: http://www.cplusplus.com/articles/z6vU7k9E/ – lurker Feb 27 '22 at 17:53
  • @lurker I won't send you to articles. Read them all. The use case is calling a method and share values, which is technically done by a pointer, also in C++ – Sam Ginrich Feb 27 '22 at 21:29
  • 1
    @SamGinrich I think I understand what you are saying. Generically speaking, if you are passing an address, the address *refers* to a value (stated in the vernacular) and thus it is a *reference*. But from a definition in computer languages, *by reference* has more meaning than that. Here's one very simple difference. If I pass a pointer to a function, it would be very wise of me in that function to check if the pointer is NULL. If I pass a reference, no such check is required since a reference, by definition in the language, cannot be null. – lurker Feb 28 '22 at 01:39
  • @lurker sure, common reference semantics in C++ suggests, that you can assume a reference to be defined by some instantiated value and thus not being a null reference. Else you can construct a reference from a pointer (in C++ e.g. int * p; int& r = *p), it can be *(int * )0 and on access will raise a null pointer ecception. I.e. pointer and reference behave equivalent, where the precise equivalence is between ´int * const p´ and ´int & r´, when &r==p – Sam Ginrich Mar 01 '22 at 13:41

1 Answers1

3

Pass by pointer is actually pass by value, there is no such concept of passing by reference in C.

In C, pass by reference == pass by pointer.

void double_ptr(int *x_ptr)
{
      *x_ptr *=2;
}
int main()
{
      int x=5;
      double_ptr(&x); /* a value is being passed*/
}

Pass by reference (in C++):

void double_ptr(int& x_ptr)
{
   x_ptr *=2;
}
int main()
{
     int x=5;
     int &x_ptr=x;
     double_ptr(x_ptr); /* no temporary is being created i.e. no extra memory allocation is here*?
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Kaustav Ray
  • 744
  • 6
  • 20
  • Try compile second version code in a C compiler like GCC, show this error: `error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token void double_ptr(int& x_ptr)` which means C does not support defining a function like that. – EsmaeelE Jan 09 '19 at 21:41
  • Concepts of passing values and sharing values existed long time ago. In the C-version of double_ptr(), *x_ptr actually is a reference (accordingly in C++ you can initialize a reference with it). Now, to say that referenced **integer** is passed by value appears awkward to me: For this you need to confuse the integer with the pointer to it. – Sam Ginrich Feb 25 '22 at 09:05