C++大學(xué)教程(大一學(xué)習課件)Mooc06-1_第1頁
C++大學(xué)教程(大一學(xué)習課件)Mooc06-1_第2頁
C++大學(xué)教程(大一學(xué)習課件)Mooc06-1_第3頁
C++大學(xué)教程(大一學(xué)習課件)Mooc06-1_第4頁
C++大學(xué)教程(大一學(xué)習課件)Mooc06-1_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

Control

StatementChapter

8Lecture

6-1OBJECTIVESIn

this

chapter

you’ll

learn:What

pointers

are.

The

similarities

and

differences

between

pointers

and

references

andwhen

to

use

each.To

use

pointers

to

pass

arguments

to

functions

by

reference.To

use

pointer-based

C-style

strings.The

close

relationships

among

pointers,

arrays

and

C-style

strings.To

use

pointers

to

functions.To

declare

and

use

arrays

of

C-style

strings.01INTRODUCTION

OF

POINTERContentsLecture

6-101

Introduction

of

pointerPointersPowerful,

but

difficult

to

masterCan

be

used

to

create

and

manipulate

dynamic

data

structuresClose

relationship

with

arrays

and

stringsPointer

variablesContain

memory

addresses

as

valuesNormally,

variable

contains

specific

value

(direct

reference)

Pointers

contain

address

of

variable

that

has

specific

value(indirect

reference)IndirectionReferencing

value

through

pointer01

Introduction

of

pointer(CONT.)PointersPowerful,

but

difficult

to

masterCan

be

used

to

create

and

manipulate

dynamic

data

structuresClose

relationship

with

arrays

and

stringsPointer

variablesContain

memory

addresses

as

valuesNormally,

variable

contains

specific

value

(direct

reference)

Pointers

contain

address

of

variable

that

has

specific

value(indirect

reference)IndirectionReferencing

value

through

pointerPointer

declarations*

indicates

variable

is

a

pointerExampleint

*myPtr;Declares

pointer

to

int,

of

type

int

*Multiple

pointers

require

multiple

asterisks int

*myPtr1,

*myPtr2;Pointer

initializationInitialized

to

0,

NULL,

or

an

address0

or

NULL

points

to

nothing

(null

pointer)01

Introduction

of

pointer(CONT.)Fig.8.1

|

Directly

and

indirectly

referencing

a

variable.Address

operator

(&)Returns

memory

address

of

its

operandExample

int

y

=

5;int

*yPtr;yPtr

=

&y;assigns

the

address

of

variable

y

to

pointer

variable

yPtrVariable

yPtr

“points

to”

yyPtr

indirectly

references

variable

y’s

value01

Introduction

of

pointer(CONT.)*

operatorAlso

called

indirection

operator

or

dereferencing

operatorReturns

synonym

for

the

object

its

operand

points

to*yPtr

returns

y

(because

yPtr

points

to

y)Dereferenced

pointer

is

an

lvalue*yptr

=

9;*

and

&

are

inverses

of

each

other

Will

“cancel

one

another

out”

when

applied

consecutively

ineither

order901

Introduction

of

pointer(CONT.)fig08_04.cpp*

and

&

are

inversesof

each

otherVariable

aPtr

isa

point

to

an

intAddress

of

a

and

the

valueof

aPtr

are

identical*

and

&

are

inverses;

same

resultwhen

both

are

applied

to

aPtrInitialize

aPtr

with

theaddVraelsuseooffvaarainadbtlheeadereferencedaPtr

are

identical02

Passing

Arguments

to

Functions

byReference

with

PointersThree

ways

to

pass

arguments

to

a

functionPass-by-valuePass-by-reference

withreference

argumentsPass-by-reference

with

pointer

argumentsA

function

can

return

only

one

valueArguments

passed

to

a

function

using

reference

argumentsFunction

can

modify

original

values

of

argumentsMore

than

one

value

“returned”02

Passing

Arguments

to

Functions

byReference

with

Pointers(CONT.)Pass-by-reference

with

pointerargumentsSimulates

pass-by-referenceUse

pointers

and

indirection

operatorPass

address

of

argument

using

&

operator

Arrays

not

passed

with

&

because

array

name

is

already

apointer

*

operatorusedas

alias/nickname

for

variable

inside

offunctionfig08_06.cppPass

number

by

value;

resultreturned

by

cubeByValuecubeByValue

receivesparameter

passed-by-valueCubes

local

variable

nand

return

the

resultFig.

8.8

|

Pass-by-value

analysis

of

the

program

ofFig.

8.6.Prototype

indicates

parameteris

a

pointer

to

an

intfig08_07.cppApply

address

operator

&

topass

address

of

number

tocubeByReferencecubeByReferencemodifies

variable

numberModify

and

access

intvariable

using

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論