Docsity

Pointer Passing in C: Understanding Passing Pointers to Functions, Study notes of Software Engineering

Partial preview of the text

Download Pointer Passing in C: Understanding Passing Pointers to Functions and more Study notes Software Engineering in PDF only on Docsity!

POINTERS IN C,

PASSING POINTERS TO

FUNCTIONS

CSSE 120—Rose-Hulman Institute of Technology

Parameter Passing Styles

 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

Evaluation of the Analogy

 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

Passing by pointers - Part 1

 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.

Passing by Point - Part 3

 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);

A simple example

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

Pointers

 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

Assignments to Pointers

 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 = #

Visualizing Pointers

Box and Pointer Diagrams

int num = 4; int *pNum; pNum = #

4

pNum: num: memory:

Both, num and *pNum are 4.

???

Summary of Pointers

 Example of a pointer variable: *pNum

 Example of a integer variable: num

 Assigning a value to an int: num = 4;

 Obtaining the address of a variable: &num

 Assigning an address to a pointer variable:

pNum = #

 Assigning a value to the variable that a pointer

variable points to:

*pNum = 7;

Summary of Pointers

 What happens, if we do:

pNum = 7 instead of:

pNum = &num

 What happens, if we do:

*pNum = &num instead of:

*pNum = 7

Practice with Pointers

**1. int x = 3, y = 5;

  1. int *px = &x;
  2. int *py = &y;
  3. printf("%d %d\n", x, y);
  4. *px = 10;
  5. printf("%d %d\n", x, y); /* x is changed */
  6. px = py;
  7. printf("%d %d\n", x, y); /* x not changed */
  8. *px = 12;
  9. printf("%d %d\n", x, y); /* y is changed /*

Break

 Starring Binky!

 (See http://cslibrary.stanford.edu/104/)