














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















CSSE 120—Rose-Hulman Institute of Technology
Most programming languages offer several means of passing parameters.
We have seen pass-by-value, in which the argument is evaluated and a copy of the value is assigned to the formal parameter of the called function.
We will now explore a second style
In the second case, the person receiving the wallet may: Take 5 bucks. Take all the money you have. Take no money Put money into your wallet
We will now introduce you to passing a parameter by pointer.
If you think of variables as wallets, then we need to have a way of passing wallets rather than the contents of a wallet.
If you prefer to think of variables as sticky notes, then we need to have a way of passing a sticky note rather than what is written on the sticky note.
The called function needs to know that it receives a wallet/sticky note rather than a value.
As such, we need to tell it.
We do this by adding the symbol ‘*’ in front of the parameter name.
If you have a parameter called y, then you change it to: *y
void foo(int *a){
*a = 7; printf("%d\n", *a); }
int b = 3; foo(&b); printf("%d\n", b);
Send the address of b
Receive an address
How do we modify downAndUp() so that it changes the values of its parameters? Together, implement a function that passes pointers to values to be changed Use project PointersInclass that you already checked out Implement downAndUpthatWorks() Use function testdownAndUpthatWorks() to test downAndUpthatWorks()
Second Example - Cont’d
Q
Variables are names of memory addresses.
Variables that we have seen so far hold values such as integers, floats, and characters.
A pointer is a variable that holds the address of some variable.
To continue our analogy, a pointer is a variable that holds a wallet or a sticky note.
Q
We need to have a way of obtaining the address of a variable, rather than its contents.
We obtain the address of a variable by using the ‘&’ (address) operator.
Example continued:
int *pNum; int num= 4; pNum = #
int num = 4; int *pNum; pNum = #
4
pNum: num: memory:
Both, num and *pNum are 4.
???
*pNum = 7;
**1. int x = 3, y = 5;
Starring Binky!
(See http://cslibrary.stanford.edu/104/)