Docsity

C++ Programming: Passing Arguments by Reference and String Manipulation, Slides of Computer Programming

Partial preview of the text

Download C++ Programming: Passing Arguments by Reference and String Manipulation and more Slides Computer Programming in PDF only on Docsity!

C++ Programming^ •

Pointers

String

docsity.com

Introduction

Pointers^ ◦

Powerful

Simulate pass-by-reference

Close relationship with arrays and strings

docsity.com

Pointer Variable Declarations andInitialization  Can declare pointers to any data type  Pointer initialization^ ◦ Initialized to 0 , NULL , or address  0 or NULL points to nothing

docsity.com

Pointer Operators

(address operator)

Returns memory address of its operand

Example

int

y

int

*yPtr;

yPtr

&y;

yPtr

gets

address

of

y

yPtr

“points to”

y

yPtr y 5 yptr 500000 600000 y 600000 5 address ofy is valueof yptr

docsity.com

code

Using the

and

operators. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9

int a;

a is an integer 11 int *aPtr;

aPtr is a pointer to an integer 1213 a

aPtr

&a;

aPtr assigned address of a 1516 cout

"The address of a is

&a 17

"\nThe value of aPtr is

aPtr; 1819 cout

"\n\nThe value of a is

a 20

"\nThe value of *aPtr is

*aPtr; 2122 cout

"\n\nShowing that

and

are inverses of

"each other.\n&*aPtr

&*aPtr 24

"\n*&aPtr

*&aPtr

endl; 25

and & are inverses of eachother

docsity.com

return

indicates successful termination 2728

end main

The

address

of

a

is

0012FED

The

value

of

aPtr

is

0012FED

The

value

of

a

is

The

value

of

*aPtr

is

Showing

that

and

are

inverses

of

each

other.

&*aPtr

0012FED

*&aPtr

0012FED

and & are inverses; same result when both appliedto aPtr

docsity.com

Calling Functions by Reference

Pass-by-reference with pointer arguments^ ◦

Simulate pass-by-reference^ 

Use pointers and indirection operator

Pass address of argument using

operator

Arrays not passed with

because array name

already pointer

operator used with alias/nickname for

variable inside of function

docsity.com

code

Cube a variable using pass-by-value. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 int cubeByValue( int

prototype 9 10 int main() 11

int number

cout

"The original value of number is

number; 1516

pass number by value to cubeByValue 17 number

cubeByValue( number

cout

"\nThe new value of number is

number

endl; 2021 return

indicates successful termination 2223

end main 24 Pass number by value;result return ed by cubeByValue

docsity.com

code

Cube a variable using pass-by-reference 3

with a pointer argument. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 void cubeByReference( int

prototype 1011 int main() 12

int number

cout

"The original value of number is

number; 1617

pass address of number to cubeByReference 18 cubeByReference( &number

cout

"\nThe new value of number is

number

endl; 2122 return

indicates successful termination 2324

end main 25 Apply address operator & to pass address of numberto cubeByReference cubeByReference modified variable number Prototype indicatesparameter is pointer to int

docsity.com

calculate cube of *nPtr; modifies variable number in main 27 void cubeByReference( int *nPtr

*nPtr

*nPtr

*nPtr

*nPtr;

cube *nPtr 3031

end function cubeByReference

The

original

value

of

number

is

The

new

value

of

number

is

cubeByReference receives address of int variable,i.e., pointer to an int Modify and access int variable usingindirection operator

docsity.com

code

Converting lowercase letters to uppercase letters 3

using a non-constant pointer to non-constant data. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 #include

prototypes for islower and toupper 1011 void convertToUppercase( char

int main() 14

char phrase[]

"characters and

cout

"The phrase before conversion is:

phrase; 18 convertToUppercase( phrase

cout

"\nThe phrase after conversion is:

phrase

endl; 2122 return

indicates successful termination 2324

end main 25 Parameter is nonconstantpointer to nonconstant data convertToUppercase modifies variable phrase

docsity.com

convert string to uppercase letters 27 void convertToUppercase( char *sPtr

while

*sPtr

'\0'

current character is not

'\0'

if

islower( *sPtr

if character is lowercase, 32 *sPtr

toupper( *sPtr

convert to uppercase 3334 ++sPtr;

move sPtr to next character in string 3536

end while 3738

end function convertToUppercase

The

phrase

before

conversion

is:

characters

and

The

phrase

after

conversion

is:

CHARACTERS

AND

Parameter sPtr nonconstant pointer tononconstant data Function islower returns true if character is lowercaseFunction toupper returns correspondinguppercase character iforiginal characterlowercase; otherwise toupper returns original When operator(uppercase) character ++ applied to pointer that points toarray, memory addressstored in pointer modifiedto point to next element ofarray.

docsity.com

sPtr cannot modify the character to which it points, 24

i.e., sPtr is a "read-only" pointer 25 void printCharacters( const char *sPtr

for

*sPtr

'\0';

sPtr++

no initialization 28 cout

*sPtr; 2930

end function printCharacters

The

string

is:

print

characters

of

a

string

sPtr is nonconstant pointer to constant data;cannot modify character towhich sPtr points. Increment sPtr to point to next character.

docsity.com

code

Attempting to modify data through a 3

non-constant pointer to constant data. 4 5 void f( const int

prototype 6 7 int main() 8

int y; 1011 f( &y

f attempts illegal modification 1213 return

indicates successful termination 1415

end main 1617

xPtr cannot modify the value of the variable 18

to which it points 19 void f( const int *xPtr

*xPtr

error: cannot modify a const object 2223

end function f

error

C2166:

l-value

specifies

const

object

Parameter is nonconstantpointer to constant data.Pass address of int variable y to attempt illegal modification.Attempt to modify const object pointed to by xPtr . Error produced whenattempting to compile.

docsity.com