# How do I implement pass-by-pointer with a pass-by-value?

Page 1 of 1

## 7 Replies - 3011 Views - Last Post: 19 March 2013 - 08:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=315746&amp;s=c3615dbb13dfd8c803e078b8525762a8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 1
• Posts: 7
• Joined: 17-March 13

# How do I implement pass-by-pointer with a pass-by-value?

Posted 17 March 2013 - 06:29 PM

Hey guys,

I often come to this site for a great deal of help for both Java and C++. I have been having a lot of trouble on my assignment for class, and I was hoping someone could nudge me in the right direction for implementing a pass-by-pointer into my program that is already using a pass-by-value. Basically, I have to use the pass-by-value mechanism to display accurate velocity calculations, then use pass-by-pointer to successfully use that result in a momentum equation. The program is suppose to display distance, time, velocity, and momentum. I am very much new to C++, and I am seriously struggling, so please do not be alarmed if my code is a bit "noob". I really only need a nudge in the right direction. Maybe an example or something? My textbook examples have me confused, and my instructor example confused me also. Here is what I have:

```//Robin Yam
//IT/218
//Velocity and Momentum Program

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

//Function prototypes (declaring the functions).
double velocity (double distance, int time);
double momentum (double velocity);

//Main function.
int main ()
{
double PrimaryVelo (0);
double TotalMomentum (0);
int t (1);
for ( double d = 1; d <= 10; d++)
{
PrimaryVelo = velocity (d, t);
}  //End for the primary for loop.
system ("pause"); //Prevents closing of debug automatically.
return 0;
} //End of the main function.

//Pass-by-value method for velocity.
double velocity (double distance, int time)
{
return (distance / time);
}

//Pass-by-pointers method for momentum.
double momentum (double velocity)
{
return (velocity * 205);
}
```

Is This A Good Question/Topic? 0

## Replies To: How do I implement pass-by-pointer with a pass-by-value?

### #2 Skydiver

• Code herder

Reputation: 4378
• Posts: 14,133
• Joined: 05-May 12

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 17 March 2013 - 07:25 PM

Is there any particular reason why you need to pass-by-reference to your momentum() function? Although you are currently still passing by value, there is no real need to pass by reference since you are returning a computed value and not changing the input parameter?

Reputation: 1
• Posts: 7
• Joined: 17-March 13

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 17 March 2013 - 09:10 PM

Skydiver, on 17 March 2013 - 07:25 PM, said:

Is there any particular reason why you need to pass-by-reference to your momentum() function? Although you are currently still passing by value, there is no real need to pass by reference since you are returning a computed value and not changing the input parameter?

It is our assignment requirement. I would not do it or have a problem if it was not a requirement, but since it is, I have to work it in. It was part of this week's learning materials, and I was unable to understand it at all.

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 18 March 2013 - 03:05 AM

There are some function tutorials in my signature that might be of assistance.

### #5 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 18 March 2013 - 07:33 AM

AdventAssassin, on 17 March 2013 - 07:29 PM, said:

Hey guys,

Basically, I have to use the pass-by-value mechanism to display accurate velocity calculations, then use pass-by-pointer to successfully use that result in a momentum equation.

OK, this is actually kind of easy. You just need to understand exactly what pass by value means and what pass by pointer means. So let's look at those two.

Pass by value:

double velocity (double distance, int time)
31 {
32 return (distance / time);
33 }

Here the function velocity accepts two variables, one of type double and one of type int. You decided to pass the values of d and t respectively to the function velocity. When passing by value, a copy of d and t are made and that is what is passed to velocity. Now velocity can change the value of it's copies of d and t (which are known as distance and time) and this of course would have no effect on the originals. In fact, distance and time are completely separate variables and have no relation to the variables d and t back in main(). It would be like if I gave you a copy of my paycheck, and then you tore it all up - I wouldn't care because it was just a copy. Often, values that are passed into a function are just disposable values, scratch pad values that are used to generate a result which can then be passed back and be caught by a waiting variable. The values that were passed in are just lost and forgotten once the function ends.

Pass by pointer:

Before we look at "pass by pointer" let's look at momentum as pass by value:

double momentum (double velocity)
37 {
38 return (velocity * 205);
39 }

So we would want to pass a copy of the value of PrimaryVelo to momentum, which would then become a separate variable known as velocity. So then we multiply velocity by 205 to get our result which is sent back to a waiting variable.

So our objective is to get the value of PrimaryVelo and multiply it by 205. Another way to get the value of PrimaryVelo is to just point to it, with a pointer. First we set up a pointer to PrimaryVelo, then we pass that pointer to the function momentum. A pointer is just another variable that contains the address of another variable. Then, whenever we use that pointer we are really using the variable itself (the one the pointer points to). Here is how you would setup a pointer to PrimaryVelo:

double* Pri_V = &PrimaryVelo; // this reads that we have a pointer to a double (double* Pri_V = &PrimaryVelo;) and it's name is Pri_V (double* Pri_V = &PrimaryVelo;) and it is loaded with the address of PrimaryVelo (double* Pri_V = &PrimaryVelo;). The "&" means "the address of". So &PrimaryVelo means the address of PrimaryVelo.

So now we have a pointer called Pri_V which points to the location of PrimaryVelo. Now in order to actually manipulate the value stored in PrimaryVelo we have to de-reference the pointer by using an asterisk before the pointer:

x = *Pri_V // Here x takes on the value of PrimaryVelo.
*Pri_V = 10; // here PrimaryVelo takes on the value of 10.
Pri_V = Pri_V + 1: Here the pointer Pri_V now points to the next memory location, an no longer points to PrimaryVelo.

So if we have a pointer to a variable then we can treat that pointer as if it were the variable itself, if we put an asterisk before it like so: *Pri_V. Any operation done on *Pri_V is actually done on PrimaryVelo, and any operation done on Pri_V is done on the pointer itself, that is, what the pointer points to is changed.

So for an example, you would first create a pointer to PrimaryVelo then pass that pointer to your function momentum:

```double momentum(double*); // prototype which takes a pointer to a double as argument.

int main()
{
double PrimaryVelo = 0;
double* p = &PrimaryVelo; //"&" causes the address of PrimaryVelo to load into pointer p;

y = momentum(p);
return 0;
}

double momentum(double* Pri_V)
{
return (*Pri_v * 205);
}
```

Remember, if we mess with Pri_V then we are messing with what variable is being pointed to; if we mess with *Pri_v then we are messing with the contents or value of whatever variable is being pointed to. This: (*Pri_V * 205) is exactly the same thing as (PrimaryVelo * 205) and thus the value of PrimaryVelo is not changed - just used. If I gave you a copy of my paycheck, and you tore it all up, I wouldn't care. But if I gave you a pointer to my paycheck, and you tore the *pointer all up, then my paycheck would have gotten torn up (pointers can be dangerous!). Think of a pointer as a voodoo doll. Once the voodoo doll is setup to represent someone, then poking the doll with a pin will actually poke the real person with a pin (even if they are all the way across town). So changing the value of voodoo changes the person to which the doll references. Then, using *voodoo allows you to actually afflict the person. Here is how you would stick it to John:

```person voodoo1 = John;
person voodoo2 = jill;
person* ptr; // ptr is a pointer to type person;
ptr = &voodoo1; // ptr now is setup with the GPS coordinates of voodoo1 (i.e. John)
*ptr = stick!! // John gets stuck with a pin.
ptr = ptr + 1; // voodoo pointer ptr now points to next memory location (Jill);
*ptr = stick!! // Jill gets stuck with a pin.

```

And just a note, when you pass a pointer to a function, it is passed by value. That is to say, Pri_V is just a copy of p and is thus a new separate disposable variable from pointer p. And a reference is similar to a pointer, but what it points to can't be changed. A reference is like a voodoo doll which cannot be changed to reference another person. If you want to represent another person, you have to have another dedicated voodoo doll for that. Research references, they are easier to use than pointers but you can't change what they point to.

This post has been edited by Mrk: 18 March 2013 - 07:41 AM

### #6 jjl

• Engineer

Reputation: 1130
• Posts: 4,686
• Joined: 09-June 09

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 18 March 2013 - 06:55 PM

Quote

```person voodoo1 = John;
person voodoo2 = jill;
person* ptr; // ptr is a pointer to type person;
ptr = &voodoo1; // ptr now is setup with the GPS coordinates of voodoo1 (i.e. John)
*ptr = stick!! // John gets stuck with a pin.
ptr = ptr + 1; // voodoo pointer ptr now points to next memory location (Jill);
*ptr = stick!! // Jill gets stuck with a pin.

```

There is no guarantee that stack variables are continuous in memory. If you want that guarantee, then you need to use an array. Using pointers to increment through stack variables is undefined behavior.

This post has been edited by jjl: 18 March 2013 - 06:55 PM

Reputation: 1
• Posts: 7
• Joined: 17-March 13

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 18 March 2013 - 11:39 PM

Mrk, on 18 March 2013 - 07:33 AM, said:

OK, this is actually kind of easy. You just need to understand exactly what pass by value means and what pass by pointer means. So let's look at those two.

You have no idea how much you helped me! You explained everything in a way I understood, and from there, I was able to get the code to work I got my momentum and velocity showing and calculating appropriately. I really cannot express how thankful I am for your help; it definitely was a push in the right direction.

### #8 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: How do I implement pass-by-pointer with a pass-by-value?

Posted 19 March 2013 - 08:01 PM

AdventAssassin, on 19 March 2013 - 12:39 AM, said:

You have no idea how much you helped me!

Glad I could help - freely receive freely give. Thanks for the feedback, as I never know if I am coming across clearly
or if I'm coming across as some mumbling bonzo.