-6

I would like to know difference between the following 2 functions

void fun1(vector<int>&v){  
v.push(1);  
fun1(v);  

}


void fun2(int *sum) 
{
 x=2;
 new_sum=*sum+x;
 fun2(&new_sum);
}

I am unable to understand difference. In first function, function declaration has use of pointer and no other place in function body has '&' symbol. In function 2, function declaration has value at address pointer and function body has '&'. Please explain

like here in below function we use &diagonalsum for passing map why it is used?

void diagonalSumUtil(struct Node* root,
            int vd, map<int, int> &diagonalSum)
{
   if(!root)
    return;

diagonalSum[vd] += root->data;

// increase the vertical distance if left child
diagonalSumUtil(root->left, vd + 1, diagonalSum);

// vertical distance remains same for right child
diagonalSumUtil(root->right, vd, diagonalSum);
}
  • c has no references and there is no function pointer in your code – 463035818_is_not_an_ai Jun 18 '18 at 18:09
  • 1
    `fun2` will never stop recursing. – tadman Jun 18 '18 at 18:09
  • @tadman i am only concerned about pointers not about recursion. Pls explain pointers – coding_is_life Jun 18 '18 at 18:10
  • 1
    @FrançoisAndrieux Technically `fun2` never starts since it doesn't compile ;) – NathanOliver Jun 18 '18 at 18:10
  • 1
    You are mistaken. The first example makes no use of pointers. In c++, the same symbol can have different meanings, depending on where it's use. In this case `&` indicates a [reference](http://en.cppreference.com/w/cpp/language/reference). – François Andrieux Jun 18 '18 at 18:11
  • you are comparing an apple with on orange (both kinda broken btw). If your question is about pointer vs reference why dont you use an example where the difference between two version is only that. Anyhow there should be a duplicate somewhere – 463035818_is_not_an_ai Jun 18 '18 at 18:11
  • "i am only concerned about pointers" if thats the case, then just use references and forget about pointers for a while ;) – 463035818_is_not_an_ai Jun 18 '18 at 18:12
  • I am pretty sure OP doesn't realize that `&` in the example above has nothing to do with pointers. May be this will help: https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in – SergeyA Jun 18 '18 at 18:13
  • @user463035818 i want to understand the difference not just forget about something. – coding_is_life Jun 18 '18 at 18:13
  • Possible duplicate of [Pointer vs. Reference](https://stackoverflow.com/questions/114180/pointer-vs-reference) – 463035818_is_not_an_ai Jun 18 '18 at 18:13
  • 3
    You need to read up on pointers and the basics, [this might help](http://www.cplusplus.com/doc/tutorial/pointers/) – BenjaFriend Jun 18 '18 at 18:14
  • well sorry for being sloppy. Of course you should not forget about pointers, the thing is just that pointers are extremely overrated, every newcomer is forced to learn them just to realize later that you rarely need them in c++ – 463035818_is_not_an_ai Jun 18 '18 at 18:15
  • Does this answer your question? [What are the differences between a pointer variable and a reference variable?](//stackoverflow.com/questions/57483/90527) – outis Jul 25 '22 at 06:50

1 Answers1

1

The ampersand (&) in a declaration declares a reference type, not a pointer.

The ampersand (&) in an expression is the address of operator, which yields a pointer.

Just because the same token is used in the language for two different things does not mean they are related things. Unfortunately, especially for learners.

Stephen M. Webb
  • 1,705
  • 11
  • 18