版權(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年湖南工業(yè)職業(yè)技術(shù)學(xué)院單招綜合素質(zhì)考試模擬試題含詳細答案解析
- 2026年西安市東城第二學(xué)校教師招聘考試重點題庫及答案解析
- 2026年寧夏財經(jīng)職業(yè)技術(shù)學(xué)院單招綜合素質(zhì)筆試備考試題含詳細答案解析
- 2026年石家莊科技信息職業(yè)學(xué)院單招綜合素質(zhì)筆試備考題庫含詳細答案解析
- 2026甘肅武威古浪縣公益性崗位工作人員招聘8人考試重點題庫及答案解析
- 2026年湖南民族職業(yè)學(xué)院單招綜合素質(zhì)考試參考題庫含詳細答案解析
- 2026年甘肅能源化工職業(yè)學(xué)院單招綜合素質(zhì)筆試備考題庫含詳細答案解析
- 2026年上海對外經(jīng)貿(mào)大學(xué)單招職業(yè)技能考試模擬試題含詳細答案解析
- 2026年青海衛(wèi)生職業(yè)技術(shù)學(xué)院單招綜合素質(zhì)考試備考試題含詳細答案解析
- 2026年鎮(zhèn)江市高等??茖W(xué)校單招綜合素質(zhì)考試模擬試題含詳細答案解析
- GLP培訓(xùn)課件教學(xué)課件
- 2026四川巴中市通江產(chǎn)業(yè)投資集團有限公司及下屬企業(yè)招聘11人備考題庫(含答案詳解)
- 數(shù)據(jù)資產(chǎn)價值評估模型構(gòu)建與分析
- 市政污水管道有限空間作業(yè)方案
- 2026中國電信四川公用信息產(chǎn)業(yè)有限責任公司社會成熟人才招聘備考題庫及1套參考答案詳解
- 2026年秦皇島煙草機械有限責任公司招聘(21人)考試參考試題及答案解析
- 職場關(guān)鍵能力課件 4 時間管理
- 記賬實操-廣告?zhèn)髅焦举~務(wù)處理分錄實例
- 2026屆廣東省華南師大附中、省實驗中學(xué)、廣雅中學(xué)、深圳高級中學(xué)四校高三語文第一學(xué)期期末質(zhì)量檢測模擬試題含解析
- 2025中日友好醫(yī)院招聘3人歷年真題匯編附答案解析
- 2025年河北省高考歷史真題卷(含答案與解析)
評論
0/150
提交評論