By Jitender Yadav

2019-06-12 07:18:36 8 Comments

Why are reference variable are considered as pointer in parameter passing?

int x; 
void fun(&x);
void fun(int* y)
    ------some code----

Here, reference of variable x is passed to fun() function. Local variable y in fun() contains reference of variable x but y is declared as pointer. Why?


@SubMachine 2019-06-12 10:31:36

Here: void fun(&x); You are passing the address (and not a reference) of variable x to void fun(int* y). Since int* y is a pointer, it then points to the address of x.

@laddu 2019-06-12 07:49:11

void fun(&x);

in this line &x is "not reference variable", that is address of x variable.


int y ; int &z = y; // here &z is reference variable(a reference must initialized when it is created)

@john 2019-06-12 07:21:31

You're mistaken &x is not a 'reference variable'. It is a pointer to x, & is the address-of operator, which returns the address (i.e. a pointer) of whatever argument it's given.

It's confusing because & is used to mean a reference in type declarations but it's meaning in expressions is different.

For example

int x;
int& y = x; // here & means reference because 'int&' is a type declaration
int* z = &x; // here & means address-of because '&x' is an expression

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] When to use references vs. pointers

37 Answered Questions

14 Answered Questions

[SOLVED] Passing a 2D array to a C++ function

36 Answered Questions

[SOLVED] Why is "using namespace std;" considered bad practice?

20 Answered Questions

[SOLVED] Returning multiple values from a C++ function

  • 2008-11-26 15:19:01
  • Fred Larson
  • 290934 View
  • 214 Score
  • 20 Answer
  • Tags:   c++

4 Answered Questions

[SOLVED] What does T&& (double ampersand) mean in C++11?

7 Answered Questions

[SOLVED] How to pass objects to functions in C++?

4 Answered Questions

13 Answered Questions

[SOLVED] Are the days of passing const std::string & as a parameter over?

  • 2012-04-19 15:20:57
  • Benj
  • 164539 View
  • 573 Score
  • 13 Answer
  • Tags:   c++ c++11

28 Answered Questions

[SOLVED] What are the barriers to understanding pointers and what can be done to overcome them?

  • 2008-08-08 06:17:51
  • David McGraw
  • 73376 View
  • 443 Score
  • 28 Answer
  • Tags:   c++ c pointers

Sponsored Content