2

What is the difference between passing by reference the parameters in a function and passing pointer variables as a parameter in a function ?

Razor
  • 39
  • 1
  • 6
  • 3
    reference is nothing but pointer. So pass by reference is equal to pass by pointer. – mahendiran.b Aug 19 '14 at 04:34
  • 1
    It is at least worth reading [Passing by reference in C](http://stackoverflow.com/questions/2229498/passing-by-reference-in-c), though it is borderline as the master question for 'this is a duplicate'. – Jonathan Leffler Aug 19 '14 at 04:39
  • if p is a variable of type int. then what is the difference between &p and *p ? – Razor Aug 19 '14 at 04:47
  • If `p` is an `int`, `*p` is invalid and `&p` is the address where `p` is stored. – Jonathan Leffler Aug 19 '14 at 04:50
  • 1
    This is not a duplicate question !!! david.pfx. Read the question carefully – Razor Aug 19 '14 at 07:07
  • The duplicate question which you are mentioning is " what is the difference between pass by value and pass by reference. There is no pass by reference in C. So i am not talking about C and concentrating on c++. I don't want to know the difference between pass by value and pass by reference. – Razor Aug 19 '14 at 08:42

2 Answers2

7

There is no pass by reference in C, it's always pass by value.

C developers can emulate pass by reference, by passing the pointers to a variable and the accessing it using dereferencing within the function. Something like the following, which sets a variable to 42:

static void changeTo42 (int *pXyzzy) {
    *pXyzzy = 42;
}
:
int x = 0;
changeTo42 (&x);

Contrast that with the C++ true pass by reference, where you don't have to muck about with pointers (and especially pointers to pointers, where even seasoned coders may still occasionally curse and gnash their teeth):

static void changeTo42 (int &xyzzy) {
    xyzzy = 42;
}
:
int x = 0;
changeTo42 (x);

I would implore ISO to consider adding true references to the next C standard. Not necessarily the full capability found in C++, just something that would fix all the problems people have when calling functions.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Since C doesn't have "passing by reference" whatsoever, it might be useful if the original post clarified its terminology, although it's likely to have come from C++, which is a different language than C. – Shao Aug 19 '14 at 04:38
  • When stating "C has no pass by reference", it's useful also to point out, that passing arrays behaves as if passed by reference (important for example if converting C array argument to C++ std::array argument). – hyde Aug 19 '14 at 07:00
  • Hyde, I don't want to open up a can of worms since you never actually pass an array. You pass a pointer to the first element, hence it's still using the "emulation". – paxdiablo Aug 19 '14 at 08:07
0

You might be thinking of C++. I'll cover that below.

In C there is no passing by reference. To accomplish the same feat, you can send a pointer to a variable as an argument and dereference the pointer in the method, as shown in paxdiablo's comment.

In C++, you could accomplish the same thing if you tried to pass by reference C-style (as explained previously) or if you tried passing the arguments as such:

static void multiply(int& x){
    x * 7;
}

void main(){
    int x = 4;
    multiply(x);
}

The variable x at the end of this program would equal 28.

  • `static void multiply(int& x)` AFAIK this notation isn't valid in plain C, its only valid in C++, isn't it? – dhein Aug 19 '14 at 06:56
  • in c++ how is this pass by reference different by passing through pointers? Or, is it the same ? – Razor Aug 19 '14 at 07:04
  • @Zaibis Yes, that is only valid in C++. The way to do so in C had already been covered by another poster, so I gave the C++ version, assuming OP might have meant to pass by reference in C++ since C does not have such a feature. – Farhan Kathawala Aug 19 '14 at 07:33
  • @Razor I do not believe it is any different. I think the only issue you might run into with passing &x instead of *x is that &x in C++ cannot be NULL. Your code cannot treat parameters passed as &x as if they were pointers which may have a NULL value. – Farhan Kathawala Aug 19 '14 at 07:37