-1

I'm new in learning C++. I'm trying to understand that under which conditions we pass pointers as function argument. According to my understanding when we pass the pointer as function's argument it overwrites the originally stored value. Same is the case when we pass by reference. Is there any other reason for it? Please try to explain this using simple language so that I can understand.

// pass a reference
void Foo(int &x)
{
  x = 2;
}

//pass a pointer
void Foo_p(int *p)
{
  *p = 9;
}

// pass by value
void Bar(int x)
{
  x = 7;
}

int main()
{
  int x = 4;
  cout<< "before function call "<< x<< endl;
  Foo(x);  // x now equals 2.
  cout<< "after first call x is "<< x <<endl;
  Foo_p(&x); // x now equals 9.
  cout<< "after second call x is "<< x <<endl;
  Bar(x);  // x still equals 9.
  cout<< "after third call x is "<< x <<endl;
} 
Styx
  • 9,863
  • 8
  • 43
  • 53
rida
  • 41
  • 5
  • 3
    How are you learning? [What does your book tell you?](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – StoryTeller - Unslander Monica Sep 28 '17 at 13:46
  • 2
    *"when we pass the pointer as function's argument it overwrites the originally stored value"* - no, doing `p = new int();` inside your `Foo_p` function would not overwrite the value of `x` in `main` – UnholySheep Sep 28 '17 at 13:48
  • You don't always want to copy an object, in addition objects in C++ can be huge in size. Using pointers/references allows you to access objects without copying them, which could be the desired behavior, or think about accessing a large object through `const &`. – Jack Sep 28 '17 at 13:48

1 Answers1

3

There is no simple rule.

Instead, you must learn what pointers do and what they mean in C++, plus what they mean in your current code base.

Programming is about communicating with the compiler, and with other programmers, and with yourself in the future. The use of a pointer or a reference has different implications in all of these situations.

A pointer is a piece of paper with a street address on it. If I pass you a piece of paper with my house on it, you can use it to send a construction crew to destroy, change or rebuild my house.

A value is an actual thing. If I pass you a copy of my actual house, nothing you can do to that house will impact my original house.

A reference is an alias to a value.

The piece of paper with the house's address written on it is itself a value. I can give you another piece of paper referring to that, and using the 2nd piece of paper you can get a scribe to rewrite the first piece of paper.

With a reference, there is no value, there is just a reference to another value.

You can think of a reference as a pointer that prevents anyone from changing it and you cannot get a pointer to the pointer.

In addition, pointers, unlike references, are "nullable" -- they can be in a state where they refer to nothing. Pointers, being values themselves, can be changed; by convention, one might pass the pointer to the first element of an array of elements, plus the length, and expect the user to copy and/or modify that pointer to refer to all of the elements.

Pointers, being values, have operations you can perform on them (as opposed to what they point to). If you have an address to a house on a street, you can "get the next house on the street" (increment the pointer). Woe betide you if you ask for the house after the last house, however.

All of this is what they are. Conventionally, programmers basically never use references to hold free store allocated objects ("heap" objects), while pointers sometimes do this. In some code bases, only smart pointers "own" such references, while others only observe.

In some code bases, using a pointer in order to denote a nullable reference is valid; in others, it is anathema.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524