LazerSharks
LazerSharks

Reputation: 3155

In C++ How do references and pointers in parameters actually work? What are their set-in-stone protocol/"rule"?

Say you have

void foo(int a, int &b, int *c){...}

int main(){
   int d=0, e=1, f=2;
   foo(d, e, &f);
}

It is my understanding that the function declaration parameters must have the same type as the argument. Hence the "int" in front of a, b, and c because d, e, and f are all ints.

But does this extend (also apply) to the references and pointers, i.e. & and *? What I mean is, if you have int &b, does e have to be an address already?

I thought it did, but now I think I have the concept wrong after reading this thread: C++ functions: ampersand vs asterisk in which the code outlined in is pretty much what I have written above.

After reading the thread and the code above, it seems that the & and * in the parameter is actually not part of the "type restriction" -- i.e. it does not mean that e has to be an address, or that f has to be a pointer.

See previously I thought that whatever proceeded the paramater names a, b, c in the parameter was strictly specifying what type it must be

so you would code something like this:

void foo(int a, int &b, int *c){...}

int main(){
   int d=0, e=1, f=2;
   foo(d, &e, *f); // wrong 
}

Instead, it seems that the & in the parameter actually takes the variabe/object argument that is passed in and CHANGES or GETS its address. And the * is actually ACTING UPON the address of f (&f) which is passed as an argument and retrieving it's contents. Is this right?

Is this just the given protocol/rule for & and * placed in the parameter that one must accept?

I've read up on many articles regarding references and pointers in parameters, and they all explain very clearly that the & and * in the parameter allows one to change the value of the variable passed. However, they never go very deep into the actual procedure/protocol of the & and * when placed in the parameter.

http://www.cplusplus.com/doc/tutorial/functions2/

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Let me know if I have this conceptually right. Thank you!

Upvotes: 2

Views: 1185

Answers (3)

Deepu
Deepu

Reputation: 7610

The doubt arises because in C++ the same operator is used for different purposes in different contexts. For example the following operators have different meaning in different situations.

The operator & can mean address-of a variable or a reference to a variable. For example consider the following code,

int *ptr;    
int n=111;    
ptr=&n;

In the above code the operator & gets the address of the variable n.

Now consider the code shown below,

   int a=111;    
   int &ref = a;

Here & is used to create a reference to the variable a. You can use the variable ref to access the variable a.

The operator * can be used to create a pointer variable or to fetch the content of a variable. For example consider the following code,

int *ptr;   

In the above code the operator * is used to declare ptr as a pointer variable.

Now consider the code shown below,

int a=111,b;    
int *ptr = &a;
b=*ptr;

Here *ptr is used to fetch the content of a.

There are other operator also in C++ with different meaning in different contexts, for example,

The operator + can be a unary or binary opearator. For example + is a binary operator in the expression c=a+b; and a unary operator in the expression b=+a;

The operator - can be a unary or binary opearator. For example - is a binary operator in the expression c=a-b; and a unary operator in the expression b=-a;

Upvotes: 5

Bhupesh Pant
Bhupesh Pant

Reputation: 4349

I strongly think you need to open some book and go through it at least once. Ampersand (&) and asterisk (*) does same thing but it’s just that they look different and they do their stuff differently.

To start,

Pointers, as you know are data types that stores address, i.e. its purpose is to point to another variables..

References - two things about them. 1). References are nothing but just another name for the objects/variables to which they are referring to. 2). References has been introduced to overcome the untidy and complex syntax of pointers. See the declaration and usage of both,

int  num = 45;
int* ptr = #
int& ref = num;

both ptr and ref is pointing to num. please notice their syntax and also the way they dereference the value.

cout<<*ptr;
cout<<ref;

Now you can see yourself, what the difference is and what looks cleaner...

-Bhupesh

Upvotes: 4

Mats Petersson
Mats Petersson

Reputation: 129454

A pointer is a variable that holds an address to something. A reference is really a pointer with a nice package and plenty of sugar in the picture to make it look tasty. Inside, once you open it up, it does the same thing.

When they put the nice packaging around pointers to make references, they did make it a little safer in that a reference can't be NULL, so you don't have to check if someone used "NULL" as the input when using a reference - pointers can be NULL, so you have to check for that first [or don't, but then it may crash - which is not very good...].

The nice packaging means the compiler knows to pass an address for a reference, where if you have a pointer, you have to make sure it's an address passed in - either by using a pointer variable as the parameter, or using the address-of operator.

I'm sure the reason they used the & for "this is a reference" is related to the fact that "where passing an address here, but you don't need to do anything when passing the variable to the function, the compiler will fix it up for you".

When you declared a pointer, you use * - this applies both for variables and arguments to functions.

In other places & means "the address of" something (make this into a pointer), and * means "what this pointer points to". Again, the nice packaging means the * is hidden from view when using references, but the compiler will do those things for you.

Upvotes: 2

Related Questions