Download as pdf or txt
Download as pdf or txt
You are on page 1of 773

C Programming:

Memory & Pointers


Part 3: Pointers & Dynamic Memory

Shahid Bokhari

An Algopath eBook
www.algopath.com
ISBN 978-0-9802341-2-1

2008

c 2008 Algopath LLC. All Rights Reserved.


Copyright
C Programming:
Memory & Pointers
An Algopath eBook

c 2008 Algopath LLC. All Rights Reserved.


Copyright
Introduction DISCLAIMER: The author and publisher have used their best efforts in preparing this eBook,
Pointers & Arrays its included animations and programs, and the associated web site. The programs in this eBook
Dynamic Memory
and on the accompanying web site are for study and educational purposes only and may contain
deliberate errors designed to illustrate various programming pitfalls to the reader. These
programs are not intended to be used as a basis for designing programs for use in the real world.
The author and publisher make no warranties of any kind, expressed or implied, with regard to
the accuracy, applicability, fitness, or completeness of the documentation, animations and
programs contained in this eBook and associated web site. This eBook, its included programs
and animations, and its associated web site are provided on an “as is” basis. The author and
publisher shall not be liable in any event for incidental or consequential damages in connection
with, or arising out of, the furnishing, performance, or use of these programs, animations and
documentation. Therefore, if you wish to apply ideas contained in this eBook and its associated
web site, you are taking full responsibility for your actions.
NOTICE: This eBook is for individual use and study only. Classroom use including, but not
limited to, (a) the projection of this material on a screen or display device, (b) broadcast,
multicast or unicast of this material to one or more computer systems or (c) broadcast,
multicast or unicast of this material to one or more screens or display devices is strictly
prohibited.
A separate Instructor’s Edition of this eBook is available at www.algopath.com for those
wishing to use this material in the classroom.

Adobe
R & Adobe
R Reader
R are trademarks of Adobe Systems Inc.

UNIX is a registered trademark of The Open Group.


Linux
R is the registered trademark of Linus Torvalds in the U.S. and other countries.

Solaris is a registered trademark of Sun Microsystems, Inc.


Windows is a registered trademark of Microsoft Corporation in the United States and other
countries.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 2 / 54
C Programming:
Memory & Pointers
An Algopath eBook

Introduction
Quick Start
Prerequisites
This eBook
Audience
Exercises
Contact Us
Getting Started
A tiny program
Animation
Initializing
An Experiment Introduction
A Big Jump

Pointers & Arrays

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 3 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Quick Start
Click to jump to any topic To move through these animations, you
? Current Section
may use:
Introduction
Quick Start  Current topic
Prerequisites
the space bar, the ↑, ↓ keys, or the Page
This eBook Up, Page Down keys on your keyboard,
Audience
Other topics in Current Section or
Exercises
Contact Us most convenient: the scroll wheel on
Getting Started
your mouse, or
A tiny program
Animation
left mouse clicks on the buttons at
Initializing
An Experiment the bottom of the page.
A Big Jump

Pointers & Arrays


Try these out now!
Dynamic Memory Many pages have content lists on the left.
 Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
(topics hidden)
Try this: click on A Big Jump.
Move forward/back to the next/last page
(skipping all steps on the current page) us-
ing .
You can also use the control buttons pro-
vided by Adobe Reader, but these will not
be available in full screen mode.
Toggle to/from fullscreen with CTRL-L and
ESC.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 4 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Prerequisites

Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 5 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Prerequisites

Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 5 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Prerequisites

Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 5 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Prerequisites

Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 5 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Prerequisites

Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 5 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook

Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook

Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook

Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook

Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook

Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook

Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Audience

Introduction While this eBook presumes only a very basic knowledge of


Quick Start
Prerequisites
C programming and should be accessible to the beginning
This eBook student, it does guide the user through advanced concepts
Audience of memory and pointers behavior.
Exercises
Contact Us It will thus be of value to established programmers in ex-
Getting Started plaining the intricacies of memory and pointer behavior–
A tiny program
Animation
concepts which are often not clear, even to experts.
Initializing
An Experiment
A Big Jump

Pointers & Arrays

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 7 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Audience

Introduction While this eBook presumes only a very basic knowledge of


Quick Start
Prerequisites
C programming and should be accessible to the beginning
This eBook student, it does guide the user through advanced concepts
Audience of memory and pointers behavior.
Exercises
Contact Us It will thus be of value to established programmers in ex-
Getting Started plaining the intricacies of memory and pointer behavior–
A tiny program
Animation
concepts which are often not clear, even to experts.
Initializing
An Experiment
A Big Jump

Pointers & Arrays

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 7 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises

Introduction In addition to detailed simulations of C programs, this


Quick Start
Prerequisites
eBook challenges you with numerous interesting exercises.
This eBook
Audience Source code for most example programs and exercises are
Exercises available at our web site www.algopath.com. Solutions to
Contact Us most exercises are also available–our objective is to make
Getting Started
A tiny program
this a self-contained resource for self-study and for rein-
Animation forcement of ongoing programming courses.
Initializing
An Experiment Please ensure that you obtain the maximum benefit from
A Big Jump the exercises by attempting to solve them before you look
Pointers & Arrays at the solutions.
Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 8 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises

Introduction In addition to detailed simulations of C programs, this


Quick Start
Prerequisites
eBook challenges you with numerous interesting exercises.
This eBook
Audience Source code for most example programs and exercises are
Exercises available at our web site www.algopath.com. Solutions to
Contact Us most exercises are also available–our objective is to make
Getting Started
A tiny program
this a self-contained resource for self-study and for rein-
Animation forcement of ongoing programming courses.
Initializing
An Experiment Please ensure that you obtain the maximum benefit from
A Big Jump the exercises by attempting to solve them before you look
Pointers & Arrays at the solutions.
Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 8 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises

Introduction In addition to detailed simulations of C programs, this


Quick Start
Prerequisites
eBook challenges you with numerous interesting exercises.
This eBook
Audience Source code for most example programs and exercises are
Exercises available at our web site www.algopath.com. Solutions to
Contact Us most exercises are also available–our objective is to make
Getting Started
A tiny program
this a self-contained resource for self-study and for rein-
Animation forcement of ongoing programming courses.
Initializing
An Experiment Please ensure that you obtain the maximum benefit from
A Big Jump the exercises by attempting to solve them before you look
Pointers & Arrays at the solutions.
Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 8 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Contact Us

Introduction We hope that you will benefit from and enjoy using this
Quick Start
Prerequisites
Algopath eBook. We welcome feedback and useful sugges-
This eBook tions will happily be incorporated in future versions. Please
Audience contact us at feedback@algopath.com.
Exercises
Contact Us
Getting Started
A tiny program
Animation
Initializing
An Experiment
A Big Jump

Pointers & Arrays

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 9 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Getting started

Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 10 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Getting started

Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 10 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Getting started

Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 10 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Getting started

Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 10 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A tiny program

Introduction The tiny program given on this page serves /* tiny.c */


Quick Start
Prerequisites
to get us started with our eBook. #include <stdio.h>
This eBook We have put in small yellow balloons at int main()
Audience
various parts of the source code. These are {
Exercises
Contact Us annotations and you can obtain details of int x, y, z;
Getting Started the items they describe by simply moving x = 3;
A tiny program
your mouse over them. y = 6;
Animation
z = 7;
Initializing Try this now. printf("%d %d %d\n", x, y, z);
An Experiment
A Big Jump Although this is a very simple program, }
Pointers & Arrays there are some aspects that you may not
Dynamic Memory
have encountered before, thus it is useful
to go over the annotations we have pro-
vided.
You can move to the next page to view
this program in action.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 11 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action start program


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z;
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays

Dynamic Memory As you move through the lines of this program using the space bar, scroll wheel or
the step arrows of eBook, you will see how memory is allocated and the allocated
during the lifetime of the program and. You will also see the output, which is a
single line.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action open scope


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z;
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays

Dynamic Memory The concept of scope is very important. Whenever an opening brace “{” (also
called “curly bracket”) is encountered, a new scope is opened. The eBook indicates
a scope with a rectangular frame in which variables are allocated as desired.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action allocate x, y, z


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z; x ? y ? z ?
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays

Dynamic Memory x, y & z are allocated, that is space is set aside for them in memory. Just after a
variable is allocated, its value is an unknown (indicated by ?).

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action x set to 3


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z; x 3 y ? z ?
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays

Dynamic Memory Variables are set to specific values.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action y set to 6


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z; x 3 y 6 z ?
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays

Dynamic Memory Variables are set to specific values.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action z set to 7


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z; x 3 y 6 z 7
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays

Dynamic Memory Variables are set to specific values.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action output x, y, z


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z; x 3 y 6 z 7
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays 3 6 7
Dynamic Memory These values are output using the printf statement.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action close scope


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z;
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays 3 6 7
Dynamic Memory When the closing brace “}” is reached the scope is closed–causing memory to be
deallocated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Animation

Code Action end program


Introduction
Quick Start
/* tiny.c */
Prerequisites #include <stdio.h>
This eBook int main()
Audience
{
Exercises
Contact Us int x, y, z;
Getting Started x = 3;
A tiny program y = 6; Memory
Animation
Initializing
z = 7;
An Experiment printf("%d %d %d\n", x, y, z); Output
A Big Jump }
Pointers & Arrays 3 6 7
Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Variables: Initial Values

Introduction The program tiny.c illustrates several interesting issues.


Quick Start
Prerequisites Of particular importance is the fact that when a variable
This eBook
Audience
is initially allocated, its value is an unknown (indicated
Exercises by ?).
Contact Us
Getting Started In the C programming language we can make no as-
A tiny program sumptions about the value to which a variable is set
Animation
when allocated.
Initializing
An Experiment
In other languages, the situation is different.
A Big Jump

Pointers & Arrays It is important for you to be aware of this distinction.


Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Variables: Initial Values

Introduction The program tiny.c illustrates several interesting issues.


Quick Start
Prerequisites Of particular importance is the fact that when a variable
This eBook
Audience
is initially allocated, its value is an unknown (indicated
Exercises by ?).
Contact Us
Getting Started In the C programming language we can make no as-
A tiny program sumptions about the value to which a variable is set
Animation
when allocated.
Initializing
An Experiment
In other languages, the situation is different.
A Big Jump

Pointers & Arrays It is important for you to be aware of this distinction.


Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Variables: Initial Values

Introduction The program tiny.c illustrates several interesting issues.


Quick Start
Prerequisites Of particular importance is the fact that when a variable
This eBook
Audience
is initially allocated, its value is an unknown (indicated
Exercises by ?).
Contact Us
Getting Started In the C programming language we can make no as-
A tiny program sumptions about the value to which a variable is set
Animation
when allocated.
Initializing
An Experiment
In other languages, the situation is different.
A Big Jump

Pointers & Arrays It is important for you to be aware of this distinction.


Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Variables: Initial Values

Introduction The program tiny.c illustrates several interesting issues.


Quick Start
Prerequisites Of particular importance is the fact that when a variable
This eBook
Audience
is initially allocated, its value is an unknown (indicated
Exercises by ?).
Contact Us
Getting Started In the C programming language we can make no as-
A tiny program sumptions about the value to which a variable is set
Animation
when allocated.
Initializing
An Experiment
In other languages, the situation is different.
A Big Jump

Pointers & Arrays It is important for you to be aware of this distinction.


Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Variables: Initial Values

Introduction The program tiny.c illustrates several interesting issues.


Quick Start
Prerequisites Of particular importance is the fact that when a variable
This eBook
Audience
is initially allocated, its value is an unknown (indicated
Exercises by ?).
Contact Us
Getting Started In the C programming language we can make no as-
A tiny program sumptions about the value to which a variable is set
Animation
when allocated.
Initializing
An Experiment
In other languages, the situation is different.
A Big Jump

Pointers & Arrays It is important for you to be aware of this distinction.


Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming Output
anything about the initial values of vari-
ables in C. On system A:
Run this program on your own machine 32767 0 0
and observe the outputs. 367

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming Output
anything about the initial values of vari-
ables in C. On system B:
Run this program on your own machine 4199328 1628438944 0
and observe the outputs. 367

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming Output
anything about the initial values of vari-
ables in C. On system C:
Run this program on your own machine 2 31 2293672
and observe the outputs. 367

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
An Experiment

Introduction To illustrate the notion of uninitialized /* tinytwo.c */


Quick Start
Prerequisites
variables, program tinytwo.c prints out x, #include <stdio.h>
This eBook y, & z before and after they are initialized. int main()
Audience The output of the first print statement will {
Exercises
Contact Us
depend on the type of computer and oper- int x, y, z;
Getting Started ating system you are using. printf("%d %d %d\n", x, y, z);
A tiny program You may get different answers if you run x = 3;
Animation
the program multiple times. y = 6;
Initializing
An Experiment
When we ran tinytwo.c on three different z = 7;
A Big Jump machines we got the outputs shown. printf("%d %d %d\n", x, y, z);
Pointers & Arrays In each case, the outputs before initializa- }
Dynamic Memory tion are unpredictable.
This points out the danger in assuming
anything about the initial values of vari-
ables in C.
Run this program on your own machine
and observe the outputs.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 14 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A Big Jump

Introduction This is a dummy slide to illustrate the jump buttons.


Quick Start
Prerequisites
This eBook Click on at the bottom of the screen to jump back
Audience
Exercises
to the page you came from.
Contact Us
Getting Started
Click on at the bottom of the screen to continue
A tiny program
Animation with the next page.
Initializing
An Experiment
A Big Jump

Pointers & Arrays

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 15 / 54
C Programming:
Memory & Pointers
An Algopath eBook

Introduction

Pointers & Arrays


Intro
Array Addresses
Indexes & Pointers–I
Indexes & Pointers–II
Indexes & Pointers–III
Pointer types
Pointer experiment
Using Pointers
Array bounds
Even elements Pointers & Arrays
Copying
Reverse Copy
Flip
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 16 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Pointers and arrays are closely related and can be /* ptrArray01.c */


Introduction
used interchangeably in many situations. #include <stdio.h>
Pointers & Arrays
Intro We will be using the function getIntArray(x,M) #define M 5
Array Addresses which reads in an entire int array x of size M. This int main()
Indexes & Pointers–I
makes our programs more compact and reduces un- {
Indexes & Pointers–II
necessary detail. int x[M], *p1, *p2;
Indexes & Pointers–III
Pointer types
getIntArray(x,M);
Pointer experiment
You can implement getIntArray() by inserting p1 = &x[0];
Using Pointers the following code just before the line containing p2 = &x[3];
Array bounds main(): p1++;
Even elements
Copying void getIntArray(int *x, int n) p2++;
Reverse Copy { printf("%d\n",*p2);
Flip
int i; p2 = x;
int to char
String Length
for(i=0; i<n; i++) printf("%d\n",*p2);
Search scanf("%d",&x[i]); }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8
To save space, we will not show the above lines in
Dynamic Memory
our animations.
In this section we will not use nested variable dec-
larations and will not show the scope stack frames,
so that pointers can be seen without clutter.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 17 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Pointers and arrays are closely related and can be /* ptrArray01.c */


Introduction
used interchangeably in many situations. #include <stdio.h>
Pointers & Arrays
Intro We will be using the function getIntArray(x,M) #define M 5
Array Addresses which reads in an entire int array x of size M. This int main()
Indexes & Pointers–I
makes our programs more compact and reduces un- {
Indexes & Pointers–II
necessary detail. int x[M], *p1, *p2;
Indexes & Pointers–III
Pointer types
getIntArray(x,M);
Pointer experiment
You can implement getIntArray() by inserting p1 = &x[0];
Using Pointers the following code just before the line containing p2 = &x[3];
Array bounds main(): p1++;
Even elements
Copying void getIntArray(int *x, int n) p2++;
Reverse Copy { printf("%d\n",*p2);
Flip
int i; p2 = x;
int to char
String Length
for(i=0; i<n; i++) printf("%d\n",*p2);
Search scanf("%d",&x[i]); }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8
To save space, we will not show the above lines in
Dynamic Memory
our animations.
In this section we will not use nested variable dec-
larations and will not show the scope stack frames,
so that pointers can be seen without clutter.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 17 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Pointers and arrays are closely related and can be /* ptrArray01.c */


Introduction
used interchangeably in many situations. #include <stdio.h>
Pointers & Arrays
Intro We will be using the function getIntArray(x,M) #define M 5
Array Addresses which reads in an entire int array x of size M. This int main()
Indexes & Pointers–I
makes our programs more compact and reduces un- {
Indexes & Pointers–II
necessary detail. int x[M], *p1, *p2;
Indexes & Pointers–III
Pointer types
getIntArray(x,M);
Pointer experiment
You can implement getIntArray() by inserting p1 = &x[0];
Using Pointers the following code just before the line containing p2 = &x[3];
Array bounds main(): p1++;
Even elements
Copying void getIntArray(int *x, int n) p2++;
Reverse Copy { printf("%d\n",*p2);
Flip
int i; p2 = x;
int to char
String Length
for(i=0; i<n; i++) printf("%d\n",*p2);
Search scanf("%d",&x[i]); }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8
To save space, we will not show the above lines in
Dynamic Memory
our animations.
In this section we will not use nested variable dec-
larations and will not show the scope stack frames,
so that pointers can be seen without clutter.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 17 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Pointers and arrays are closely related and can be /* ptrArray01.c */


Introduction
used interchangeably in many situations. #include <stdio.h>
Pointers & Arrays
Intro We will be using the function getIntArray(x,M) #define M 5
Array Addresses which reads in an entire int array x of size M. This int main()
Indexes & Pointers–I
makes our programs more compact and reduces un- {
Indexes & Pointers–II
necessary detail. int x[M], *p1, *p2;
Indexes & Pointers–III
Pointer types
getIntArray(x,M);
Pointer experiment
You can implement getIntArray() by inserting p1 = &x[0];
Using Pointers the following code just before the line containing p2 = &x[3];
Array bounds main(): p1++;
Even elements
Copying void getIntArray(int *x, int n) p2++;
Reverse Copy { printf("%d\n",*p2);
Flip
int i; p2 = x;
int to char
String Length
for(i=0; i<n; i++) printf("%d\n",*p2);
Search scanf("%d",&x[i]); }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8
To save space, we will not show the above lines in
Dynamic Memory
our animations.
In this section we will not use nested variable dec-
larations and will not show the scope stack frames,
so that pointers can be seen without clutter.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 17 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action start program


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

p1, p2 are pointers to int–they can be used to point to int array elements as well.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action open scope


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

p1, p2 are pointers to int–they can be used to point to int array elements as well.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action allocate x[5], p1, p2


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 × p2 × x[0] ? x[1] ? x[2] ? x[3] ? x[4] ?

Memory

p1, p2 are pointers to int–they can be used to point to int array elements as well.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action input x[ ];


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 × p2 × x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

getIntArray(x,M) reads in the entire array x

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action p1 = &x[0];


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 × x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

p1 is set to point to x[0], by loading it with &x[0], the address of x[0].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action p2 = &x[3];


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 • x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

p2 set to point to x[3].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action p1++;


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 • x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

Incrementing p1 causes it to point to the element that follows x[0], i.e. x[1].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action p2++;


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M);
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 • x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

Incrementing p2 causes it to point to the element that follows x[3], i.e. x[4].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action output *p2;


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M); 8
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 • x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

Print the pointee of p2 i.e. x[4].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action p2 = x;
Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M); 8
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 • x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

p2 is set to x = address of x[0].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action output *p2;


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M); 8
Using Pointers p1 = &x[0]; 2
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

p1 • p2 • x[0] 2 x[1] 9 x[2] 1 x[3] 7 x[4] 8

Memory

Print the pointee of p2 which is now x[0].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action close scope


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M); 8
Using Pointers p1 = &x[0]; 2
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

In C, the name of an array is a pointer and points to the first element of the array.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Array Addresses

Code Action END PROGRAM


Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M); 8
Using Pointers p1 = &x[0]; 2
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

In C, the name of an array is a pointer and points to the first element of the array.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action start program


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

We can access the elements of array d using array indexing, that is d[0], d[1], . . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action open scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

We can access the elements of array d using array indexing, that is d[0], d[1], . . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action allocate i, d[4], ptr


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] ? d[1] ? d[2] ? d[3] ? i ?

Memory

We can access the elements of array d using array indexing, that is d[0], d[1], . . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action input d[ ];


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i ?

Memory

We can access the elements of array d using array indexing, that is d[0], d[1], . . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action for loop


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output d[i];


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action for loop


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output d[i];


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action for loop


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output d[i];


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action for loop


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0];
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output d[i];


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action for loop


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This for loop prints out array elements in a familiar way that we have used in prior
examples.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action ptr set to &d[0]


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

The same result can be obtained by setting ptr to point to the start of the array d
and then printing *ptr, incrementing ptr, and so on.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action open scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output *ptr;


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action increment ptr


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action close scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action open scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output *ptr;


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action increment ptr


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action close scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action open scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++;
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output *ptr;


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action increment ptr


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action close scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action open scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search }
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action output *ptr;


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search } -6
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action increment ptr


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search } -6
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

Notice that at the end of the while loop, ptr points to i, which happens to be at
the location after d[3].

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action close scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search } -6
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

The while loop terminates when ptr points beyond d[3], so this location is never
accessed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action open scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search } -6
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action close scope


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search } -6
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–I

Code Action END PROGRAM


Introduction
/* ptrArray02.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = &d[0]; -6
Reverse Copy while(ptr < &d[MAX]){ 7
Flip printf("%d\n",*ptr);
int to char
8
String Length
ptr++; 1
Search } -6
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action start program


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action open scope


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action allocate i, d[4], ptr


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] ? d[1] ? d[2] ? d[3] ? i ?

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action input d[ ];


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i ?

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action for loop


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output d[i];


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action for loop


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output d[i];


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action for loop


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]);
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output d[i];


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action for loop


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d;
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output d[i];


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action for loop


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action ptr set to d


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action while(ptr < d+4 )


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX )
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output *ptr ;


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action while(ptr < d+4 )


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output *ptr ;


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action while(ptr < d+4 )


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
}
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output *ptr ;


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
} 1
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action while(ptr < d+4 )


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
} 1
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action output *ptr ;


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
} 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action while(ptr < d+4 )


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
} 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action close scope


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
} 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–II

Code Action END PROGRAM


Introduction
/* ptrArray03.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX], *ptr; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying ptr = d; -6
Reverse Copy while(ptr < d+MAX ) 7
Flip printf("%d\n",*ptr++ );
int to char
8
String Length
} 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory

This program shows that &d[max] (used in ptrArray02.c) can be replaced by


d+MAX: both mean the same thing. Furthermore, the operations printf(. . . *ptr);
followed by ptr++; can be combined in one line: printf(. . . *ptr++);.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action start program


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action open scope


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action allocate i, d[4]


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] ? d[1] ? d[2] ? d[3] ? i ?

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action input d[ ];


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i ?

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX);
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output d[i];


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++)
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output d[i];


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]);
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output d[i];


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++)
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output d[i];


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) );
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output *(di ;


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output *(di ;


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output *(di ;


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length 1
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length 1
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action output *(di ;


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action for loop


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action close scope


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Indexes & Pointers–III

Code Action END PROGRAM


Introduction
/* ptrArray04.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define MAX 4 Input
Array Addresses
Indexes & Pointers–I int main() 7 8 1 -6
Indexes & Pointers–II {
Indexes & Pointers–III
int i, d[MAX]; Output
Pointer types
Pointer experiment getIntArray(d,MAX); 7
Using Pointers for(i=0; i<MAX; i++) 8
Array bounds
printf("%d\n", d[i]); 1
Even elements
Copying for(i=0; i<MAX; i++) -6
Reverse Copy printf("%d\n",*(d+i) ); 7
Flip }
int to char
8
String Length 1
Search -6
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to different types

/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory

In this program, we have also incorrectly used


an int pointer to point to the same char array.
Incrementing the int pointer causes it to jump
4 chars at a time.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 54
Experimenting with pointer types

Code Action start program


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action open scope


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action allocate c[9] , pc


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ? c[1] ? c[2] ? c[3] ? c[4] ? c[5] ? c[6] ? c[7] ? c[8] ? pc ×


Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action allocate pi, i


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ? c[1] ? c[2] ? c[3] ? c[4] ? c[5] ? c[6] ? c[7] ? c[8] ? pc × pi × i ?

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action input c;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc × pi × i ?

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action pi set to c


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc × pi • i ?

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action pc set to c


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i ?

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c);
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c;
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c;
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 2

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 2

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++)
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 3

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 3

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++);
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 4

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 4

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++)
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 5

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 5

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++);
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 6

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 6

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
}

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 7

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pc;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 7

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 8

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pi;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r
s

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r
s

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action output *pi;


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r
s
n

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action for loop


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r
s
n

c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 2

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action close scope


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r
s
n

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
Experimenting with pointer types

Code Action END PROGRAM


/* ptrArray05.c */
#include <stdio.h>
int main() Input
{ stranger
char c[9] , *pc;
int *pi, i; Output
scanf("%s",c); s
pi = c; t
pc = c; r
for(i=0; i<8; i++) a
printf("%c\n", *pc++); n
for(i=0; i<2; i++) g
printf("%c\n", *pi++); e
} r
s
n

Memory

You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action start program
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action allocate i, d[6], ptr, tmp
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp × ptr × d[0] ? d[1] ? d[2] ? d[3] ? d[4] ? d[5] ? i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action input d[ ];
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp × ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action ptr set to d
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp × ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp × ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp × ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 8 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 1 d[3] -6 d[4] 9 d[5] 4 i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -6 d[4] 9 d[5] 4 i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -6 d[4] 9 d[5] 4 i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -6 d[4] 9 d[5] 4 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -6 d[4] 9 d[5] 4 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -6 d[4] 9 d[5] 4 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 9 d[5] 4 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 9 d[5] 4 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 9 d[5] 4 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 9 d[5] 4 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 9 d[5] 4 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 4 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 4 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 4 i 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 4 i 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 4 i 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 5 i 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 5 i 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

tmp • ptr • d[0] 8 d[1] 9 d[2] 2 d[3] -5 d[4] 10 d[5] 5 i 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Manipulating Arrays with Pointers

Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action END PROGRAM
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action start program
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action allocate s, i, a[7], p
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p × a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ? i ? s ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action input a[ ];
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p × a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i ? s ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s set to 0
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p × a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i ? s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p set to a
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i ? s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 0 s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 0 s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 0 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 0 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 0 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 1 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 1 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 1 s 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 1 s 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 1 s 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 2 s 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 2 s 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 2 s 14

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 2 s 14

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 2 s 14

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 3 s 14

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 3 s 14

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 3 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 3 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 3 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 4 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 4 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 4 s 13

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 4 s 13

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 4 s 13

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 5 s 13

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 5 s 13

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 5 s 17

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 5 s 17

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 5 s 17

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 6 s 17

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action open scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 6 s 17

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action s += *p;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 6 s 18

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action p++;
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 6 s 18

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 6 s 18

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action for loop
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M);
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 7 s 18

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action output s
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M); sum = 18
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] -3 a[5] 4 a[6] 1 i 7 s 18

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action close scope
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M); sum = 18
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Pointers and Array bounds
When using pointers to step through arrays, the pointer may point to a location outside the array,
typically at the end of a loop, as in this case. Generally speaking, no harm is done if the pointer is not
dereferenced when it is pointing to an illegal location.
Code Action END PROGRAM
/* ptrArray07.c */
#include <stdio.h>
#define M 7 Input
int main() 3 2 9 2 -3 4 1
{
int s, i, a[M], *p; Output
getIntArray(a,M); sum = 18
s = 0;
p = a;
for(i=0; i<M; i++)
{
s += *p;
p++;
}
printf("sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action start program
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action open scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action allocate s, a[8], p
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p × a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ? a[7] ? s ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action input a[ ];
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p × a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action s set to 0
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p × a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action p set to a
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action open scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action s += *p;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action p += 2;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action close scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action open scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action s += *p;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action p += 2;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action close scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action open scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action s += *p;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action p += 2;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action close scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action open scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 16

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action s += *p;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action p += 2;
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action close scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N);
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action output s
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N); even sum = 12
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

p • a[0] 3 a[1] 2 a[2] 9 a[3] 2 a[4] 4 a[5] 0 a[6] -4 a[7] 1 s 12

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action close scope
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N); even sum = 12
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Summing Even-indexed Elements of an Array
The even elements of an array are accessed by starting with a pointer pointing to index 0 and incrementing
it by 2. This may cause the pointer to point outside the array at the end of the loop, as shown.
Code Action END PROGRAM
/* ptrArray08.c */
#include <stdio.h>
#define N 8 Input
int main() 3 2 9 2 4 0 -4 1
{
int s, a[N], *p; Output
getIntArray(a,N); even sum = 12
s = 0;
p = a;
do
{
s += *p;
p += 2;
}while (p < a+N);
printf("even sum = %d\n",s);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action start program
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action open scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action allocate i, b[4], pb, a[4], pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa × a[0] ? a[1] ? a[2] ? a[3] ? pb × b[0] ? b[1] ? b[2] ? b[3] ? i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action input a[ ];
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa × a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb × b[0] ? b[1] ? b[2] ? b[3] ? i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action pa set to a
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb × b[0] ? b[1] ? b[2] ? b[3] ? i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action pb set to b
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] ? b[1] ? b[2] ? b[3] ? i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action for loop
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] ? b[1] ? b[2] ? b[3] ? i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action open scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] ? b[1] ? b[2] ? b[3] ? i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action *pb set to *pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] ? b[2] ? b[3] ? i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] ? b[2] ? b[3] ? i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pb
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] ? b[2] ? b[3] ? i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action close scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] ? b[2] ? b[3] ? i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action for loop
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] ? b[2] ? b[3] ? i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action open scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] ? b[2] ? b[3] ? i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action *pb set to *pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] ? b[3] ? i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] ? b[3] ? i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pb
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] ? b[3] ? i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action close scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] ? b[3] ? i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action for loop
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] ? b[3] ? i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action open scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] ? b[3] ? i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action *pb set to *pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] ? i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] ? i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pb
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] ? i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action close scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] ? i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action for loop
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] ? i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action open scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] ? i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action *pb set to *pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] 7 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pa
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] 7 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action increment pb
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] 7 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action close scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] 7 i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action for loop
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 pb • b[0] 4 b[1] 1 b[2] -3 b[3] 7 i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action close scope
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Copying an Array
This program shows how an array may be copied into another using pointers.
Code Action END PROGRAM
/* ptrArray09.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, b[M], *pb, a[M], *pa;
getIntArray(a,M);
pa = a;
pb = b;
for(i=0; i<M; i++)
{
*pb = *pa;
pa++;
pb++;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action start program


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action open scope


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action allocate i, pb, b[4], a[4], pa


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa × a[0] ? a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ? pb × i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action input a[ ];


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa × a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] ? b[3] ? pb × i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action pa set to a


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] ? b[3] ? pb × i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action pb set to &b[4-1]


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] ? b[3] ? pb • i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action for loop


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] ? b[3] ? pb • i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action *pb−− = *pa++ ;


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] ? b[3] 4 pb • i 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action for loop


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] ? b[3] 4 pb • i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action *pb−− = *pa++ ;


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] 1 b[3] 4 pb • i 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action for loop


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] ? b[2] 1 b[3] 4 pb • i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action *pb−− = *pa++ ;


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] -3 b[2] 1 b[3] 4 pb • i 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action for loop


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] ? b[1] -3 b[2] 1 b[3] 4 pb • i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action *pb−− = *pa++ ;


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] 7 b[1] -3 b[2] 1 b[3] 4 pb • i 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action for loop


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

pa • a[0] 4 a[1] 1 a[2] -3 a[3] 7 b[0] 7 b[1] -3 b[2] 1 b[3] 4 pb • i 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action close scope


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reverse Copy
Reverse copying is done by incrementing one pointer and decrementing the other. At the end of the loop,
both pointers are out of array bounds, but we do not dereference them, so there is no illegal access.

Code Action END PROGRAM


/* ptrArray10.c */
#include <stdio.h>
#define M 4 Input
int main() 4 1 -3 7
{
int i, *pb, b[M], a[M], *pa;
getIntArray(a,M);
pa = a;
pb = &b[M-1];
for(i=0; i<M; i++)
*pb-- = *pa++ ;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action start program
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action allocate a[8], pH, pL, t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t ? pL × pH × a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ? a[7] ?

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action input a[ ];
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t ? pL × pH × a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 6

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action pL set to a
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t ? pL • pH × a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 6

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action pH set to &a[8-1]
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t ? pL • pH • a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 6

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t ? pL • pH • a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 6

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t ? pL • pH • a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 6

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 6

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 4 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 6 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 9 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 1 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 9 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] 3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] -3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 3 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 5 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 7 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 7 a[4] 7 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 5 a[4] 7 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 5 a[4] 7 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 5 a[4] 7 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

t 5 pL • pH • a[0] 6 a[1] 9 a[2] 3 a[3] 5 a[4] 7 a[5] -3 a[6] 1 a[7] 4

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action END PROGRAM
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 29 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action start program
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action allocate pC , dC , c[3]
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

c[0] ? c[1] ? c[2] ? dC ? pC ×


Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action allocate dI , i[3], pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI × i[0] ? i[1] ? i[2] ? dI ? c[0] ? c[1] ? c[2] ? dC ? pC ×


Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action input i[ ];
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI × i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ? c[1] ? c[2] ? dC ? pC ×


Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action pI set to i
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ? c[1] ? c[2] ? dC ? pC ×


Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action pC set to c
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ? c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ? c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ? c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action *pC set to (char)*pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pC
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ? c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action *pC set to (char)*pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pC
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ? dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action *pC set to (char)*pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pC
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action END PROGRAM
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}

Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 30 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action start program
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action allocate c[8] , pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

pC × c[0] ? c[1] ? c[2] ? c[3] ? c[4] ? c[5] ? c[6] ? c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action allocate len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len ? pC × c[0] ? c[1] ? c[2] ? c[3] ? c[4] ? c[5] ? c[6] ? c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action input c;
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len ? pC × c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action pC set to c
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len ? pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action len set to 0
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 0 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 0 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 0 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action open scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment len
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action increment pC
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action while(*pC 6= NULL)
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action output len;
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len; 6
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action close scope
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len; 6
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action END PROGRAM
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len; 6
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 31 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action start program
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action open scope
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action allocate val, d[7], ptr
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr × d[0] ? d[1] ? d[2] ? d[3] ? d[4] ? d[5] ? d[6] ? val ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action input d[ ];
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action input val;
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action ptr set to &d[0]
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action while(ptr < &d[7])
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action if(*ptr equals val)
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action increment ptr
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action while(ptr < &d[7])
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action if(*ptr equals val)
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action increment ptr
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action while(ptr < &d[7])
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action if(*ptr equals val)
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action increment ptr
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action while(ptr < &d[7])
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action if(*ptr equals val)
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action increment ptr
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action while(ptr < &d[7])
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action if(*ptr equals val)
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action increment ptr
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action while(ptr < &d[7])
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action if(*ptr equals val)
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action open scope
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val);
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action print ”found”;
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val); found
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action break;
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val); found
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action close scope
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val); found
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 d[4] 7 d[5] -3 d[6] 0 val -3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
Linear Search
This program loads an array and reads in a value val. It then looks at the elements of the array in a linear
or sequential fashion and, if val is in the array, prints the message “found”.
Code Action END PROGRAM
/* ptrArray14.c */
#include <stdio.h>
#define MAX 7 Input
int main() 7 8 1 -6 7 -3 0
{ -3
int val, d[MAX], *ptr;
getIntArray(d,MAX); Output
scanf("%d",&val); found
ptr = &d[0];
while(ptr < &d[MAX])
if(*ptr == val)
{
printf("found");
break;
}
else
ptr++;
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Binary Search

The preceding program used linear search to /* ptrArray15.c */


Introduction
step through an array looking for a value. #include <stdio.h>
Pointers & Arrays We now present binary search which can be #define MAX 8
Intro used if the data stored in the array is sorted or int main()
Array Addresses
arranged in ascending order. {
Indexes & Pointers–I
int *Lo, d[MAX], *Hi, *mid, val;
Indexes & Pointers–II We use pointers Lo and Hi to a portion of the getIntArray(d,MAX);
Indexes & Pointers–III
Pointer types
array. scanf("%d",&val);
Pointer experiment Initially, Lo points to the 0th element, d[0] and Lo = &d[0];
Using Pointers
Array bounds
Hi to the last element, d[MAX–1]. Hi = &d[MAX-1];
Even elements At each pass through the while loop, a test is while(Hi - Lo > 1)
Copying
made to see if the desired element val, is less {
Reverse Copy
than the value stored at the halfway location mid = Lo + (Hi - Lo)/2;
Flip
int to char between Lo and Hi, called mid. if(val < *mid)
String Length Hi = mid;
Search If it is less, the Hi pointer is decreased to mid, else
Binary Search otherwise the Lo pointer is increased to mid. Lo = mid;
Exercises PA1-PA5
Exercises PA6-PA8 At each step, the range from Lo to Hi is halved }
Dynamic Memory
in size, and this proceeds until Lo is next to Hi. if (val == *Lo)
Unlike linear search, binary search does not printf("yes");
look at all elements of the array and is therefore }
much faster than linear search.
In the animation that follows on the next page,
remember that Hi & Lo are array locations, not
the values at those locations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 54
Code Action start program
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action open scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action allocate Lo, d[8], Hi, mid, val
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo × d[0] ? d[1] ? d[2] ? d[3] ? d[4] ? d[5] ? d[6] ? d[7] ? mid × Hi × val ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action input d[ ];
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo × d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi × val ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action input val;
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo × d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi × val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action Lo set to &d[0]
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi × val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action Hi set to &d[8-1]
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action while(Hi - Lo > 1)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action open scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action mid set to Lo + (Hi - Lo)/2
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action if(val < *mid)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action Lo set to mid
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action close scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action while(Hi - Lo > 1)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action open scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action mid set to Lo + (Hi - Lo)/2
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action if(val < *mid)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action Lo set to mid
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action close scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action while(Hi - Lo > 1)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action open scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action mid set to Lo + (Hi - Lo)/2
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action if(val < *mid)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action Hi set to mid
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action close scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action while(Hi - Lo > 1)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action if (val equals *Lo)
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val);
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action print ”yes”;
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val); yes
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action close scope
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val); yes
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
Code Action END PROGRAM
/* ptrArray15.c */
#include <stdio.h>
#define MAX 8 Input
int main() -3 -2 0 1 2 5 9 12
{ 5
int *Lo, d[MAX], *Hi, *mid, val;
getIntArray(d,MAX); Output
scanf("%d",&val); yes
Lo = &d[0];
Hi = &d[MAX-1];
while(Hi - Lo > 1)
{
mid = Lo + (Hi - Lo)/2;
if(val < *mid)
Hi = mid;
else
Lo = mid;
}
if (val == *Lo)
printf("yes");
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises PA1-PA5

Exercise PA1: Modify ptrArray06.c so that it only uses variables i and d[MAX].
Introduction
Exercise PA2: Modify ptrArray07.c so that the for loop contains only one
Pointers & Arrays
statement.
Intro
Array Addresses
Indexes & Pointers–I
Exercise PA3: Modify ptrArray08.c so that it sums the odd elements of a[M].
Indexes & Pointers–II
Indexes & Pointers–III
Exercise PA4: Modify ptrArray13.c so that it does not require the variable len and
Pointer types the for loop contains only one statement.
Pointer experiment
Using Pointers Exercise PA5: The search program ptrArray14.c reports only if an element exists
Array bounds in an array. Enhance this program so that it also prints out the index where the
Even elements
Copying
element was found and not found otherwise.
Reverse Copy
Flip
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8

Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises PA6-PA8

Exercise PA6: The following incomplete program reads in 3 strings and prints them
Introduction
out in dictionary order. Complete the nested if statement. No other changes can be
Pointers & Arrays
made. This program is about 40 lines long and may have only the one printf shown.
Intro
#include <stdio.h>
Array Addresses
#include <string.h>
Indexes & Pointers–I
Indexes & Pointers–II
#define MAX 3
Indexes & Pointers–III int main()
Pointer types {
Pointer experiment char a[MAX], b[MAX], c[MAX], *p1, *p2, *p3;
Using Pointers scanf("%s",a); scanf("%s",b); scanf("%s",c);
Array bounds if(...)
Even elements {...}
Copying printf("%s %s %s\n", p1, p2, p3);
Reverse Copy
}
Flip
int to char
String Length
Exercise PA7: Modify program ptrArray05.c so that it prints out the addresses
Search stored in the pointers pC and pI at each step. For pC this could be done as follows.
Binary Search
Exercises PA1-PA5
printf("%p %c\n", pc, *pc);
Exercises PA6-PA8 pc++;
Dynamic Memory Verify that the char pointer increases by one location at a time, while the int
pointer increases by a different amount (this will depend on your computer system).
Exercise PA8: Modify ptrArray09.c to print out the addresses contained in the
pointers at each step. Also print out the sizes of chars, ints and pointers using the
sizeof() function, e.g. printf(”%d”, sizeof(pa));. Are the addresses consistent with
the sizes? The answer to this question depends on the way your computer lays out
variables in memory.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 36 / 54
C Programming:
Memory & Pointers
An Algopath eBook

Introduction

Pointers & Arrays

Dynamic Memory
Intro-I
Intro-II
Example
In Action
malloc() & free()
Memory Leak
Allocating blocks
Malloc Failure
Run time allocation
Dynamic Memory
Swapping Pointers
Pointers to pointers
Exercises D1–D5
Exercises D6–D8

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-I

Introduction

Pointers & Arrays


Dynamic memory is a very powerful and, at the same
time, very hazardous part of C programming.
Dynamic Memory
Intro-I
It is powerful because it provides a very flexible mech-
Intro-II
Example anism for allocating and deallocating memory based on
In Action conditions that arise while a program is running.
malloc() & free()
Memory Leak For example, you may want to allocate an array based
Allocating blocks on the size of an input data set. This information would
Malloc Failure
Run time allocation
vary each time the program was run, so you could not fix
Swapping Pointers the size of an array at the time of writing the program
Pointers to pointers (i.e. at compile time).
Exercises D1–D5
Exercises D6–D8 You would like the array to be allocated when the pro-
gram is in execution (i.e. at run time).
We shall see how dynamic memory permits this capabil-
ity.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-I

Introduction

Pointers & Arrays


Dynamic memory is a very powerful and, at the same
time, very hazardous part of C programming.
Dynamic Memory
Intro-I
It is powerful because it provides a very flexible mech-
Intro-II
Example anism for allocating and deallocating memory based on
In Action conditions that arise while a program is running.
malloc() & free()
Memory Leak For example, you may want to allocate an array based
Allocating blocks on the size of an input data set. This information would
Malloc Failure
Run time allocation
vary each time the program was run, so you could not fix
Swapping Pointers the size of an array at the time of writing the program
Pointers to pointers (i.e. at compile time).
Exercises D1–D5
Exercises D6–D8 You would like the array to be allocated when the pro-
gram is in execution (i.e. at run time).
We shall see how dynamic memory permits this capabil-
ity.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-I

Introduction

Pointers & Arrays


Dynamic memory is a very powerful and, at the same
time, very hazardous part of C programming.
Dynamic Memory
Intro-I
It is powerful because it provides a very flexible mech-
Intro-II
Example anism for allocating and deallocating memory based on
In Action conditions that arise while a program is running.
malloc() & free()
Memory Leak For example, you may want to allocate an array based
Allocating blocks on the size of an input data set. This information would
Malloc Failure
Run time allocation
vary each time the program was run, so you could not fix
Swapping Pointers the size of an array at the time of writing the program
Pointers to pointers (i.e. at compile time).
Exercises D1–D5
Exercises D6–D8 You would like the array to be allocated when the pro-
gram is in execution (i.e. at run time).
We shall see how dynamic memory permits this capabil-
ity.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-I

Introduction

Pointers & Arrays


Dynamic memory is a very powerful and, at the same
time, very hazardous part of C programming.
Dynamic Memory
Intro-I
It is powerful because it provides a very flexible mech-
Intro-II
Example anism for allocating and deallocating memory based on
In Action conditions that arise while a program is running.
malloc() & free()
Memory Leak For example, you may want to allocate an array based
Allocating blocks on the size of an input data set. This information would
Malloc Failure
Run time allocation
vary each time the program was run, so you could not fix
Swapping Pointers the size of an array at the time of writing the program
Pointers to pointers (i.e. at compile time).
Exercises D1–D5
Exercises D6–D8 You would like the array to be allocated when the pro-
gram is in execution (i.e. at run time).
We shall see how dynamic memory permits this capabil-
ity.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-I

Introduction

Pointers & Arrays


Dynamic memory is a very powerful and, at the same
time, very hazardous part of C programming.
Dynamic Memory
Intro-I
It is powerful because it provides a very flexible mech-
Intro-II
Example anism for allocating and deallocating memory based on
In Action conditions that arise while a program is running.
malloc() & free()
Memory Leak For example, you may want to allocate an array based
Allocating blocks on the size of an input data set. This information would
Malloc Failure
Run time allocation
vary each time the program was run, so you could not fix
Swapping Pointers the size of an array at the time of writing the program
Pointers to pointers (i.e. at compile time).
Exercises D1–D5
Exercises D6–D8 You would like the array to be allocated when the pro-
gram is in execution (i.e. at run time).
We shall see how dynamic memory permits this capabil-
ity.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-II

Introduction

Pointers & Arrays


Dynamic memory is hazardous because the C program-
ming language imposes no control over the way you use
Dynamic Memory
Intro-I it.
Intro-II
Example With careless programming, it is easy to lose track of
In Action where, and how much dynamic memory you have allo-
malloc() & free() cated.
Memory Leak
Allocating blocks Furthermore, if you do not diligently deallocate memory
Malloc Failure
Run time allocation
that you allocate, you may cause the system to run out
Swapping Pointers of available space.
Pointers to pointers
Exercises D1–D5 Algopath eBook technology allows us to explore these
Exercises D6–D8 issues in great detail.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-II

Introduction

Pointers & Arrays


Dynamic memory is hazardous because the C program-
ming language imposes no control over the way you use
Dynamic Memory
Intro-I it.
Intro-II
Example With careless programming, it is easy to lose track of
In Action where, and how much dynamic memory you have allo-
malloc() & free() cated.
Memory Leak
Allocating blocks Furthermore, if you do not diligently deallocate memory
Malloc Failure
Run time allocation
that you allocate, you may cause the system to run out
Swapping Pointers of available space.
Pointers to pointers
Exercises D1–D5 Algopath eBook technology allows us to explore these
Exercises D6–D8 issues in great detail.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-II

Introduction

Pointers & Arrays


Dynamic memory is hazardous because the C program-
ming language imposes no control over the way you use
Dynamic Memory
Intro-I it.
Intro-II
Example With careless programming, it is easy to lose track of
In Action where, and how much dynamic memory you have allo-
malloc() & free() cated.
Memory Leak
Allocating blocks Furthermore, if you do not diligently deallocate memory
Malloc Failure
Run time allocation
that you allocate, you may cause the system to run out
Swapping Pointers of available space.
Pointers to pointers
Exercises D1–D5 Algopath eBook technology allows us to explore these
Exercises D6–D8 issues in great detail.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Intro-II

Introduction

Pointers & Arrays


Dynamic memory is hazardous because the C program-
ming language imposes no control over the way you use
Dynamic Memory
Intro-I it.
Intro-II
Example With careless programming, it is easy to lose track of
In Action where, and how much dynamic memory you have allo-
malloc() & free() cated.
Memory Leak
Allocating blocks Furthermore, if you do not diligently deallocate memory
Malloc Failure
Run time allocation
that you allocate, you may cause the system to run out
Swapping Pointers of available space.
Pointers to pointers
Exercises D1–D5 Algopath eBook technology allows us to explore these
Exercises D6–D8 issues in great detail.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A simple Example
When using dynamic memory we must
/*dynamic01.c*/
include the file <stdlib.h>, which con-
Introduction #include <stdio.h>
tains the facilities for handling dynamic
Pointers & Arrays #include <stdlib.h>
memory.
Dynamic Memory int main()
Intro-I Dynamic memory can only be accessed {
Intro-II through pointers: we first declare a int *x, *y;
Example
In Action
pointer and then allocate a block of dy- x = (int *) malloc(sizeof(int));
malloc() & free() namic memory that is a pointee. y = (int *) malloc(sizeof(int));
Memory Leak *x = 3;
Allocating blocks To allocate dynamic memory, we use
*y = 2;
Malloc Failure the malloc() (memory allocate) func-
Run time allocation printf("%d %d\n",*x, *y);
tion. In the present example, the first
Swapping Pointers *y = *x * *y;
Pointers to pointers malloc allocates enough memory to
printf("%d %d\n",*x, *y);
Exercises D1–D5 store an int. This memory is pointed
Exercises D6–D8 free(y);
to by x.
free(x);
This memory can be referenced by us- }
ing an expression such as *x = 3, which
means “set the location pointed to by
x to 3.”
You can use *x just as you would an
ordinary int variable.
Once you have finished with a dynamic
variable, you must deallocate it, using
the function free()

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A simple Example
When using dynamic memory we must
/*dynamic01.c*/
include the file <stdlib.h>, which con-
Introduction #include <stdio.h>
tains the facilities for handling dynamic
Pointers & Arrays #include <stdlib.h>
memory.
Dynamic Memory int main()
Intro-I Dynamic memory can only be accessed {
Intro-II through pointers: we first declare a int *x, *y;
Example
In Action
pointer and then allocate a block of dy- x = (int *) malloc(sizeof(int));
malloc() & free() namic memory that is a pointee. y = (int *) malloc(sizeof(int));
Memory Leak *x = 3;
Allocating blocks To allocate dynamic memory, we use
*y = 2;
Malloc Failure the malloc() (memory allocate) func-
Run time allocation printf("%d %d\n",*x, *y);
tion. In the present example, the first
Swapping Pointers *y = *x * *y;
Pointers to pointers malloc allocates enough memory to
printf("%d %d\n",*x, *y);
Exercises D1–D5 store an int. This memory is pointed
Exercises D6–D8 free(y);
to by x.
free(x);
This memory can be referenced by us- }
ing an expression such as *x = 3, which
means “set the location pointed to by
x to 3.”
You can use *x just as you would an
ordinary int variable.
Once you have finished with a dynamic
variable, you must deallocate it, using
the function free()

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A simple Example
When using dynamic memory we must
/*dynamic01.c*/
include the file <stdlib.h>, which con-
Introduction #include <stdio.h>
tains the facilities for handling dynamic
Pointers & Arrays #include <stdlib.h>
memory.
Dynamic Memory int main()
Intro-I Dynamic memory can only be accessed {
Intro-II through pointers: we first declare a int *x, *y;
Example
In Action
pointer and then allocate a block of dy- x = (int *) malloc(sizeof(int));
malloc() & free() namic memory that is a pointee. y = (int *) malloc(sizeof(int));
Memory Leak *x = 3;
Allocating blocks To allocate dynamic memory, we use
*y = 2;
Malloc Failure the malloc() (memory allocate) func-
Run time allocation printf("%d %d\n",*x, *y);
tion. In the present example, the first
Swapping Pointers *y = *x * *y;
Pointers to pointers malloc allocates enough memory to
printf("%d %d\n",*x, *y);
Exercises D1–D5 store an int. This memory is pointed
Exercises D6–D8 free(y);
to by x.
free(x);
This memory can be referenced by us- }
ing an expression such as *x = 3, which
means “set the location pointed to by
x to 3.”
You can use *x just as you would an
ordinary int variable.
Once you have finished with a dynamic
variable, you must deallocate it, using
the function free()

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A simple Example
When using dynamic memory we must
/*dynamic01.c*/
include the file <stdlib.h>, which con-
Introduction #include <stdio.h>
tains the facilities for handling dynamic
Pointers & Arrays #include <stdlib.h>
memory.
Dynamic Memory int main()
Intro-I Dynamic memory can only be accessed {
Intro-II through pointers: we first declare a int *x, *y;
Example
In Action
pointer and then allocate a block of dy- x = (int *) malloc(sizeof(int));
malloc() & free() namic memory that is a pointee. y = (int *) malloc(sizeof(int));
Memory Leak *x = 3;
Allocating blocks To allocate dynamic memory, we use
*y = 2;
Malloc Failure the malloc() (memory allocate) func-
Run time allocation printf("%d %d\n",*x, *y);
tion. In the present example, the first
Swapping Pointers *y = *x * *y;
Pointers to pointers malloc allocates enough memory to
printf("%d %d\n",*x, *y);
Exercises D1–D5 store an int. This memory is pointed
Exercises D6–D8 free(y);
to by x.
free(x);
This memory can be referenced by us- }
ing an expression such as *x = 3, which
means “set the location pointed to by
x to 3.”
You can use *x just as you would an
ordinary int variable.
Once you have finished with a dynamic
variable, you must deallocate it, using
the function free()

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A simple Example
When using dynamic memory we must
/*dynamic01.c*/
include the file <stdlib.h>, which con-
Introduction #include <stdio.h>
tains the facilities for handling dynamic
Pointers & Arrays #include <stdlib.h>
memory.
Dynamic Memory int main()
Intro-I Dynamic memory can only be accessed {
Intro-II through pointers: we first declare a int *x, *y;
Example
In Action
pointer and then allocate a block of dy- x = (int *) malloc(sizeof(int));
malloc() & free() namic memory that is a pointee. y = (int *) malloc(sizeof(int));
Memory Leak *x = 3;
Allocating blocks To allocate dynamic memory, we use
*y = 2;
Malloc Failure the malloc() (memory allocate) func-
Run time allocation printf("%d %d\n",*x, *y);
tion. In the present example, the first
Swapping Pointers *y = *x * *y;
Pointers to pointers malloc allocates enough memory to
printf("%d %d\n",*x, *y);
Exercises D1–D5 store an int. This memory is pointed
Exercises D6–D8 free(y);
to by x.
free(x);
This memory can be referenced by us- }
ing an expression such as *x = 3, which
means “set the location pointed to by
x to 3.”
You can use *x just as you would an
ordinary int variable.
Once you have finished with a dynamic
variable, you must deallocate it, using
the function free()

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 54
C Programming:
Memory & Pointers
An Algopath eBook
A simple Example
When using dynamic memory we must
/*dynamic01.c*/
include the file <stdlib.h>, which con-
Introduction #include <stdio.h>
tains the facilities for handling dynamic
Pointers & Arrays #include <stdlib.h>
memory.
Dynamic Memory int main()
Intro-I Dynamic memory can only be accessed {
Intro-II through pointers: we first declare a int *x, *y;
Example
In Action
pointer and then allocate a block of dy- x = (int *) malloc(sizeof(int));
malloc() & free() namic memory that is a pointee. y = (int *) malloc(sizeof(int));
Memory Leak *x = 3;
Allocating blocks To allocate dynamic memory, we use
*y = 2;
Malloc Failure the malloc() (memory allocate) func-
Run time allocation printf("%d %d\n",*x, *y);
tion. In the present example, the first
Swapping Pointers *y = *x * *y;
Pointers to pointers malloc allocates enough memory to
printf("%d %d\n",*x, *y);
Exercises D1–D5 store an int. This memory is pointed
Exercises D6–D8 free(y);
to by x.
free(x);
This memory can be referenced by us- }
ing an expression such as *x = 3, which
means “set the location pointed to by
x to 3.”
You can use *x just as you would an
ordinary int variable.
Once you have finished with a dynamic
variable, you must deallocate it, using
the function free()

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 54
Pointers in Action
Code Action start program
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
The memory of a computer may be considered
to be partitioned into two areas.
The first area is stack memory where frames are
allocated and allocated in step with scopes, as if
on a stack. This is the type of memory we have
dealt with so far in this eBook.
The second area is dynamic memory, where
memory is allocated and deallocated dynamically
under the programmer’s control.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action open scope
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
Dynamic memory is always accessed though
pointers.
In the simplest situation, shown here, the point-
ers are located in stack memory while the
pointees are in dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action allocate *x, *y
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x × y×
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
int *x, *y; allocates space in stack memory for × = unallocated pointer, does not point to anything.
two int pointers. At this stage they do not point
to anything.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action malloc x
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x • y × ?
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
malloc() allocates memory sufficient for an int × = unallocated pointer, does not point to anything.
variable in dynamic memory.
• = allocated pointer, points to memory allocated in
x is the pointer and *x is the variable pointed to,
sometimes called the pointee. *x can be used dynamic memory.
like an ordinary int variable.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action malloc y
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
x • y • ?
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
Similarly, y is the pointer and *y is the pointee. × = unallocated pointer, does not point to anything.
*y can be used like an ordinary int variable.
• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action *x set to 3
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
x • y • 3
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
For example, *x and *y can be set to values, . . . × = unallocated pointer, does not point to anything.
• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action *y set to 2
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
x • y • 3
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y);
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
For example, *x and *y can be set to values, . . . × = unallocated pointer, does not point to anything.
• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action output *x, *y
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
x • y • 3
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
For example, *x and *y can be set to values, . . . × = unallocated pointer, does not point to anything.
be printed out, . . .
• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action *y set to *x * *y
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h> 6
int main()
{
x • y • 3
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y;
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
For example, *x and *y can be set to values, . . . × = unallocated pointer, does not point to anything.
be printed out, . . .
• = allocated pointer, points to memory allocated in
and can can take part in arithmetic expressions.
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action output *x, *y
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h> 6
int main()
{
x • y • 3
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y; 3 6
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
For example, *x and *y can be set to values, . . . × = unallocated pointer, does not point to anything.
be printed out, . . .
• = allocated pointer, points to memory allocated in
and can can take part in arithmetic expressions.
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action free(y) y set to NULL
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x • y × 3
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y; 3 6
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
When we no longer need a dynamic variable, we × = unallocated pointer, does not point to anything.
must deallocate it using free().
• = allocated pointer, points to memory allocated in
After deallocation, *y disappears and y does not
point anywhere. The pointer exists, but there is dynamic memory.
no pointee.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action free(x) x set to NULL
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x × y×
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y; 3 6
printf("%d %d\n",*x, *y);
free(y);
free(x);
}
All variables that are allocated must be freed. × = unallocated pointer, does not point to anything.
• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action close scope
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y; 3 6
printf("%d %d\n",*x, *y);
free(y);
free(x);
}

× = unallocated pointer, does not point to anything.


• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
Pointers in Action
Code Action END PROGRAM
/*dynamic01.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y;
x = (int *) malloc(sizeof(int)); Stack Memory Dynamic Memory
y = (int *) malloc(sizeof(int));
*x = 3;
Output
*y = 2;
printf("%d %d\n",*x, *y); 3 2
*y = *x * *y; 3 6
printf("%d %d\n",*x, *y);
free(y);
free(x);
}

× = unallocated pointer, does not point to anything.


• = allocated pointer, points to memory allocated in
dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 54
malloc & free
Code Action start program
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
It is the programmers responsibility to free every malloced variable.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action open scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
In the present example, we ensure that each malloc() is matched with a corresponding free().

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action allocate *x, *y, z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x × y× z ?
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action malloc x
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y ×z ? ?
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action malloc y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
?
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z ? ?
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *x set to 3
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
?
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z ? 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *y set to 2
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z ? 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action output *x, *y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z ? 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action for loop
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action open scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action allocate *y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
y × 2
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
mallocs inside nested scopes must be separately freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action malloc y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The inner pointer y is not the same as the outer y–they belong to different scopes, even though the
allocated pointees live in the same dynamic memory.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *y set to z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 0
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The inner y must be freed separately from the outer y.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *x set to 1+z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 0
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y;
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action output *x, *y, z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 0
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action free(y) y set to NULL
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
y × 2
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The inner y is malloced inside a loop, and must be freed each time the loop block ends.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action close scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 0 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action for loop
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action open scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action allocate *y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
y × 2
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action malloc y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *y set to z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 1
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 1
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *x set to 1+z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 1
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int));
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action output *x, *y, z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 1
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action free(y) y set to NULL
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
y × 2
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action close scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 1 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action for loop
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action open scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action allocate *y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
y × 2
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action malloc y
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *y set to z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 2
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action *x set to 1+z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z;
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action output *x, *y, z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
y • 2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action free(y) y set to NULL
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
y × 2
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action close scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 2 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action for loop
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 3 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action output *x, *y, z
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
2
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y • z 3 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z; 3 2 3
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action free(y) y set to NULL
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x • y ×z 3 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z; 3 2 3
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The outer x and y are separately freed at the end of the program.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action free(x) x set to NULL
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int)); x × y× z 3
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z; 3 2 3
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The outer x and y are separately freed at the end of the program.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action close scope
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z; 3 2 3
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The outer x and y are separately freed at the end of the program.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
malloc & free
Code Action END PROGRAM
/*dynamic03.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
Stack Memory Dynamic Memory
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) Output
{ 3 2
int *y; 1 0 0
y = (int *)malloc(sizeof(int)); 2 1 1
*y = z; 3 2 2
*x = 1+z; 3 2 3
printf("%d %d %d\n",*x, *y, z);
free(y);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}
The outer x and y are separately freed at the end of the program.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 54
A Memory Leak
Code Action start program
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++)
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

We now show the disastrous consequences of omitting a free inside a loop.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action open scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++)
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action allocate *x, *y, z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x × y× z ?
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action malloc x
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y ×z ? ?
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action malloc y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; ?
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z ? ?
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *x set to 3
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; ?
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z ? 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *y set to 2
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z ? 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z);
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action output *x, *y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z ? 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action for loop
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action open scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action allocate *y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y × 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action malloc y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
?
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

The inner y is malloced. . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *y set to z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

The inner y is malloced. . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *x set to 1+z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
}
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

The inner y is malloced. . .

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action output *x, *y, z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

. . . but not freed at the end of the loop.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action close scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 0 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

This causes the pointee to be left isolated in dynamic memory, with nothing pointing to it (as the original
pointer disappears when the scope closes).

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action for loop
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action open scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action allocate *y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y × 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action malloc y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
?
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *y set to z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 1
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *x set to 1+z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z);
free(y);
free(x);
}

Each time the loop executes a new pointee is left isolated dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action output *x, *y, z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

If this loop were running for millions or billions of iterations, all dynamic memory would be exhausted.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action close scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 1 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

No further mallocs would be possible and the program would fail.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action for loop
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action open scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action allocate *y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y × 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action malloc y
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *y set to z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 2
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action *x set to 1+z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y);
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action output *x, *y, z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; y • 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action close scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 2 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action for loop
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 3 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x);
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action output *x, *y, z
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2; 2
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y • z 3 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x); 3 2 3
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action free(y) y set to NULL
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x • y ×z 3 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x); 3 2 3
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action free(x) x set to NULL
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++) x × y× z 3
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x); 3 2 3
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action close scope
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h> 2
int main()
{
1
int *x, *y, z;
x = (int *)malloc(sizeof(int));
0
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++)
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x); 3 2 3
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
A Memory Leak
Code Action END PROGRAM
/*dynamic04.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *x, *y, z;
x = (int *)malloc(sizeof(int));
y = (int *)malloc(sizeof(int));
*x = 3;
*y = 2;
printf("%d %d\n",*x, *y);
for(z=0; z<3; z++)
{
int *y; Stack Memory Dynamic Memory
y = (int *)malloc(sizeof(int));
*y = z; Output
*x = 1+z;
printf("%d %d %d\n",*x, *y, z); 3 2
} 1 0 0
printf("%d %d %d\n",*x, *y, z); 2 1 1
free(y); 3 2 2
free(x); 3 2 3
}

This is a simple example of what is often called a memory leak.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating blocks of memory
The malloc() function can be
/*dynamic05.c*/
used to allocate blocks of dy-
Introduction #include <stdio.h>
namic memory.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory In this example, two blocks of 8 int main()
Intro-I and 5 chars are allocated. {
Intro-II char *a, *b, *p, *q;
Example Once blocks have been allocated,
In Action
int i;
they can be accessed or manipu-
malloc() & free() a = (char *) malloc (8 *sizeof(char) );
Memory Leak
lated using the same techniques scanf("%s",a);
Allocating blocks that were used for arrays and b = (char *) malloc (5 *sizeof(char) );
Malloc Failure pointers in earlier parts of this
Run time allocation p = a;
eBook.
Swapping Pointers q = b;
Pointers to pointers
Exercises D1–D5
In this example, a and b are for(i=0; i<4; i++)
Exercises D6–D8 treated as if they were arrays. *q++ = *p++;
*q = NULL;
The first 4 items of a are copied printf("a=%s b=%s\n", a, b);
over to b. free(b);
A NULL is placed at the free(a);
end of b, making b a proper }
string,that can be printed out us-
ing printf(”%s”,...).
At the end of the program, the
allocated memory is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 54
Code Action start program
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

Stack Memory Dynamic Memory


malloc() can be used to allocate blocks of dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action open scope
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

Stack Memory Dynamic Memory


malloc() can be used to allocate blocks of dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action allocate a, b, p, q
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

a × b× p× q×
Stack Memory Dynamic Memory
malloc() can be used to allocate blocks of dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action allocate i
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

i ? a × b× p× q×
Stack Memory Dynamic Memory
malloc() can be used to allocate blocks of dynamic memory.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action malloc a
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

i ? a • b × p× q× ? ? ? ? ? ? ? ?

Stack Memory Dynamic Memory


a points to the first char in a block of 8 chars.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action input a;
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

i ? a • b × p× q× ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


a can be considered an array: here we use scanf() to read data into it.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action malloc b
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

? ? ? ? ?

i ? a • b • p × q× ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


b points to the first char in a block of 5 chars.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action p set to a
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

? ? ? ? ?

i ? a • b • p • q × ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


Pointer p is set to point to the block pointed to by a.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action q set to b
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

? ? ? ? ?

i ? a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


Pointer q is set to point to the block pointed to by b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action for loop
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

? ? ? ? ?

i 0 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action *q++ = *p++;
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’ ? ? ? ?

i 0 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action for loop
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’ ? ? ? ?

i 1 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action *q++ = *p++;
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’ ? ? ?

i 1 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action for loop
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’ ? ? ?

i 2 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action *q++ = *p++;
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’’r’ ? ?

i 2 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action for loop
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’’r’ ? ?

i 3 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action *q++ = *p++;
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’’r’’a’ ?

i 3 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action for loop
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’’r’’a’ ?

i 4 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


This loop copies the first 4 items of a to b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action *q set to NULL
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) );
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’’r’’a’’\0’

i 4 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


A NULL is put in the last location of b so as to make it a proper string.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action output a, b
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) ); a=abracad b=abra
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

’a’’b’’r’’a’’\0’

i 4 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


Since a and b are strings, they can be printed out using printf(”%s”...).

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action free(b) b set to NULL
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) ); a=abracad b=abra
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

i 4 a • b ×p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’

Stack Memory Dynamic Memory


b and a freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action free(a) a set to NULL
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) ); a=abracad b=abra
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

i 4 a × b× p • q •

Stack Memory Dynamic Memory


p and q still point to the (now non-existent) addresses of a and b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action close scope
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) ); a=abracad b=abra
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

Stack Memory Dynamic Memory


We could have set p and q to NULLs to avoid confusion, if this code were part of a larger program.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
Code Action END PROGRAM
/*dynamic05.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
abracad
{
char *a, *b, *p, *q; Output
int i;
a = (char *) malloc (8 *sizeof(char) ); a=abracad b=abra
scanf("%s",a);
b = (char *) malloc (5 *sizeof(char) );
p = a;
q = b;
for(i=0; i<4; i++)
*q++ = *p++;
*q = NULL;
printf("a=%s b=%s\n", a, b);
free(b);
free(a);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Malloc Failure

Introduction

Pointers & Arrays


If malloc is successful in allocating the requested block of memory, it will return
a pointer to the first location in that block.
Dynamic Memory
Intro-I
If it is not succesful, it will return NULL.
Intro-II
Example
malloc can fail if you request a block that is larger than the available dynamic
In Action
malloc() & free() memory.
Memory Leak
Allocating blocks
This could obviously happen if your request is exessively large, for example if you
Malloc Failure request a 350MB block on a computer that has only 256MB memory.
Run time allocation
Swapping Pointers A more realistic situation is when you request a reasonably sized block but your
Pointers to pointers system has lost most or all of its dynamic memory because of a memory leak. In
Exercises D1–D5
Exercises D6–D8
this case it is important to realize that you can proceed no further.
You can check for a malloc failure as follows:
a = (int *)malloc(1000*sizeof(int));
if(a==NULL){
printf(”error: malloc failed”);
exit(1);
}
Here exit(1) will terminate program execution after printing the error message.
We have not used this kind of check in our simple demonstration programs to
save space.
You should always use this check in any reasonably large program.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 46 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating arrays at run time
malloc() permits us to allocate
/*dynamic06.c*/
arrays at run time, as opposed to
Introduction #include <stdio.h>
compile time.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory You may not know how large an int main()
Intro-I array would be required when you {
Intro-II
write a program, as that may de- int *a, *p, i, c, n;
Example
In Action pend on the input to the pro- scanf("%d", &n);
malloc() & free() gram. a = (int *) malloc (n *sizeof(int) );
Memory Leak p = a;
Allocating blocks You could therefor not use ordi- getIntArray(a,n);
Malloc Failure nary arrays (e.g., int a[20]) in
Run time allocation c = 0;
your program.
Swapping Pointers for(i=0; i<n; i++)
Pointers to pointers
Exercises D1–D5
Here, we first read in n from in- if(*p++ < 0)
Exercises D6–D8 put and then malloc a block of c++;
n ints, which is effectively an int printf("%d\n",c);
array. p = NULL;
free(a);
We subsequently count the num- }
ber of negative elements in this
array and print it out.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating arrays at run time
malloc() permits us to allocate
/*dynamic06.c*/
arrays at run time, as opposed to
Introduction #include <stdio.h>
compile time.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory You may not know how large an int main()
Intro-I array would be required when you {
Intro-II
write a program, as that may de- int *a, *p, i, c, n;
Example
In Action pend on the input to the pro- scanf("%d", &n);
malloc() & free() gram. a = (int *) malloc (n *sizeof(int) );
Memory Leak p = a;
Allocating blocks You could therefor not use ordi- getIntArray(a,n);
Malloc Failure nary arrays (e.g., int a[20]) in
Run time allocation c = 0;
your program.
Swapping Pointers for(i=0; i<n; i++)
Pointers to pointers
Exercises D1–D5
Here, we first read in n from in- if(*p++ < 0)
Exercises D6–D8 put and then malloc a block of c++;
n ints, which is effectively an int printf("%d\n",c);
array. p = NULL;
free(a);
We subsequently count the num- }
ber of negative elements in this
array and print it out.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating arrays at run time
malloc() permits us to allocate
/*dynamic06.c*/
arrays at run time, as opposed to
Introduction #include <stdio.h>
compile time.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory You may not know how large an int main()
Intro-I array would be required when you {
Intro-II
write a program, as that may de- int *a, *p, i, c, n;
Example
In Action pend on the input to the pro- scanf("%d", &n);
malloc() & free() gram. a = (int *) malloc (n *sizeof(int) );
Memory Leak p = a;
Allocating blocks You could therefor not use ordi- getIntArray(a,n);
Malloc Failure nary arrays (e.g., int a[20]) in
Run time allocation c = 0;
your program.
Swapping Pointers for(i=0; i<n; i++)
Pointers to pointers
Exercises D1–D5
Here, we first read in n from in- if(*p++ < 0)
Exercises D6–D8 put and then malloc a block of c++;
n ints, which is effectively an int printf("%d\n",c);
array. p = NULL;
free(a);
We subsequently count the num- }
ber of negative elements in this
array and print it out.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating arrays at run time
malloc() permits us to allocate
/*dynamic06.c*/
arrays at run time, as opposed to
Introduction #include <stdio.h>
compile time.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory You may not know how large an int main()
Intro-I array would be required when you {
Intro-II
write a program, as that may de- int *a, *p, i, c, n;
Example
In Action pend on the input to the pro- scanf("%d", &n);
malloc() & free() gram. a = (int *) malloc (n *sizeof(int) );
Memory Leak p = a;
Allocating blocks You could therefor not use ordi- getIntArray(a,n);
Malloc Failure nary arrays (e.g., int a[20]) in
Run time allocation c = 0;
your program.
Swapping Pointers for(i=0; i<n; i++)
Pointers to pointers
Exercises D1–D5
Here, we first read in n from in- if(*p++ < 0)
Exercises D6–D8 put and then malloc a block of c++;
n ints, which is effectively an int printf("%d\n",c);
array. p = NULL;
free(a);
We subsequently count the num- }
ber of negative elements in this
array and print it out.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Allocating arrays at run time
malloc() permits us to allocate
/*dynamic06.c*/
arrays at run time, as opposed to
Introduction #include <stdio.h>
compile time.
Pointers & Arrays #include <stdlib.h>
Dynamic Memory You may not know how large an int main()
Intro-I array would be required when you {
Intro-II
write a program, as that may de- int *a, *p, i, c, n;
Example
In Action pend on the input to the pro- scanf("%d", &n);
malloc() & free() gram. a = (int *) malloc (n *sizeof(int) );
Memory Leak p = a;
Allocating blocks You could therefor not use ordi- getIntArray(a,n);
Malloc Failure nary arrays (e.g., int a[20]) in
Run time allocation c = 0;
your program.
Swapping Pointers for(i=0; i<n; i++)
Pointers to pointers
Exercises D1–D5
Here, we first read in n from in- if(*p++ < 0)
Exercises D6–D8 put and then malloc a block of c++;
n ints, which is effectively an int printf("%d\n",c);
array. p = NULL;
free(a);
We subsequently count the num- }
ber of negative elements in this
array and print it out.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 54
Code Action start program
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

Stack Memory Dynamic Memory


We will first read in n and then allocate a block of n ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action open scope
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

Stack Memory Dynamic Memory


We will first read in n and then allocate a block of n ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action allocate a, p, i, c, n
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i ? c ? n ? a × p×
Stack Memory Dynamic Memory
We will first read in n and then allocate a block of n ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action input n;
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i ? c ? n 8 a × p×
Stack Memory Dynamic Memory
n is read in.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action malloc a
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i ? c ? n 8 a • p × ? ? ? ? ? ? ? ?

Stack Memory Dynamic Memory


a points to a block of n ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action p set to a
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i ? c ? n 8 a • p • ? ? ? ? ? ? ? ?

Stack Memory Dynamic Memory


a points to a block of n ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action input a[ ];
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i ? c ? n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


a can be manipulated like an int array of dimension n.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action c set to 0
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i ? c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


a can be manipulated like an int array of dimension n.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 0 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 0 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 1 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 1 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action c++;
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 1 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 2 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 2 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 3 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 3 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action c++;
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 3 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 4 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 4 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 5 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 5 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 6 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 6 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action c++;
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 6 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 7 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


This loop counts the number of negative elements, c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action if(*p++ < 0)
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 7 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


At this time p points to the nonexistent memory location after a[7]–but we never dereference p, so all is
well.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action for loop
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a;
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 8 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


At this time p points to the nonexistent memory location after a[7]–but we never dereference p, so all is
well.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action output c;
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a; 3
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 8 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


c is now printed out.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action p set to NULL
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a; 3
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 8 c 3 n 8 a • p × 3 -9 7 -2 4 7 -6 5

Stack Memory Dynamic Memory


p set to NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action free(a) a set to NULL
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a; 3
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

i 8 c 3 n 8 a × p×
Stack Memory Dynamic Memory
a is freed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action close scope
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a; 3
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
Code Action END PROGRAM
/*dynamic06.c*/
#include <stdio.h>
#include <stdlib.h> Input
int main()
8
{
3 -9 7 -2 4 7 -6 5
int *a, *p, i, c, n;
scanf("%d", &n); Output
a = (int *) malloc (n *sizeof(int) );
p = a; 3
getIntArray(a,n);
c = 0;
for(i=0; i<n; i++)
if(*p++ < 0)
c++;
printf("%d\n",c);
p = NULL;
free(a);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers
Once memory blocks have been
/*dynamic07.c*/
allocated using malloc(), the
Introduction #include <stdio.h>
pointers to these blocks may be
Pointers & Arrays #include <stdlib.h>
manipulated as we have done in
Dynamic Memory #define M 5
earlier examples.
Intro-I int main()
Intro-II In this program we allocate two {
Example
In Action
blocks of char, read strings into char *x, *y, *z;
malloc() & free() them and then swap their point- x = (char *)malloc(M*sizeof(char));
Memory Leak ers. scanf("%s",x);
Allocating blocks
y = (char *)malloc(M*sizeof(char));
Malloc Failure The swapping of the pointers
Run time allocation scanf("%s",y);
takes a fixed amount of time,
Swapping Pointers printf("%s %s\n", x, y);
Pointers to pointers regardless of the length of the
z = x;
Exercises D1–D5 strings being swapped.
Exercises D6–D8 x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 49 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers
Once memory blocks have been
/*dynamic07.c*/
allocated using malloc(), the
Introduction #include <stdio.h>
pointers to these blocks may be
Pointers & Arrays #include <stdlib.h>
manipulated as we have done in
Dynamic Memory #define M 5
earlier examples.
Intro-I int main()
Intro-II In this program we allocate two {
Example
In Action
blocks of char, read strings into char *x, *y, *z;
malloc() & free() them and then swap their point- x = (char *)malloc(M*sizeof(char));
Memory Leak ers. scanf("%s",x);
Allocating blocks
y = (char *)malloc(M*sizeof(char));
Malloc Failure The swapping of the pointers
Run time allocation scanf("%s",y);
takes a fixed amount of time,
Swapping Pointers printf("%s %s\n", x, y);
Pointers to pointers regardless of the length of the
z = x;
Exercises D1–D5 strings being swapped.
Exercises D6–D8 x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 49 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers
Once memory blocks have been
/*dynamic07.c*/
allocated using malloc(), the
Introduction #include <stdio.h>
pointers to these blocks may be
Pointers & Arrays #include <stdlib.h>
manipulated as we have done in
Dynamic Memory #define M 5
earlier examples.
Intro-I int main()
Intro-II In this program we allocate two {
Example
In Action
blocks of char, read strings into char *x, *y, *z;
malloc() & free() them and then swap their point- x = (char *)malloc(M*sizeof(char));
Memory Leak ers. scanf("%s",x);
Allocating blocks
y = (char *)malloc(M*sizeof(char));
Malloc Failure The swapping of the pointers
Run time allocation scanf("%s",y);
takes a fixed amount of time,
Swapping Pointers printf("%s %s\n", x, y);
Pointers to pointers regardless of the length of the
z = x;
Exercises D1–D5 strings being swapped.
Exercises D6–D8 x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 49 / 54
Code Action start program
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action open scope
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action allocate x, y, z
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

x × y× z×
Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action malloc x
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

x • y × z× ? ? ? ? ?

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action input x;
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

x • y × z× ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action malloc y
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

? ? ? ? ?

x • y • z × ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action input y;
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x);
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z × ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action output x, y;
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z × ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action z set to x
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z • ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action x set to y
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z • ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action y set to z
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char));
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z • ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action output x, y;
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char)); that this
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z • ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action z set to NULL
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char)); that this
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

’t’’h’’a’’t’’\0’

x • y • z × ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action free(y) y set to NULL
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char)); that this
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

x • y × z× ’t’’h’’i’’s’’\0’

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action free(x) x set to NULL
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char)); that this
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

x × y× z×
Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action close scope
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char)); that this
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
Code Action END PROGRAM
/*dynamic07.c*/
#include <stdio.h>
#include <stdlib.h> Input
#define M 5
this
int main()
that
{
char *x, *y, *z; Output
x = (char *)malloc(M*sizeof(char));
scanf("%s",x); this that
y = (char *)malloc(M*sizeof(char)); that this
scanf("%s",y);
printf("%s %s\n", x, y);
z = x;
x = y;
y = z;
printf("%s %s\n", x, y);
z = NULL;
free(y);
free(x);
}

Stack Memory Dynamic Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to pointers
In our final example, we show
/*dynamic08.c*/
how malloc() can be used to
Introduction #include <stdio.h>
allocate pointers to pointers
Pointers & Arrays #include <stdlib.h>
which are, in turn, used to point
Dynamic Memory int main()
to real data.
Intro-I {
Intro-II This lets us handle complicated int **p , *t ;
Example
In Action
relationships between data. p = (int **) malloc (3 *sizeof(int *) );
malloc() & free() p[0] = (int *) malloc (1*sizeof(int) );
Memory Leak
In dynamic08.c we first allocate
p[1] = (int *) malloc (1*sizeof(int) );
Allocating blocks a block p of 3 locations of type
p[2] = (int *) malloc (1*sizeof(int) );
Malloc Failure int *, that is pointer to int.
Run time allocation scanf("%d %d %d",p[0],p[1],p[2]);
Swapping Pointers Note carefully that p is a printf("%d %d %d\n",*p[0],*p[1],*p[2]);
Pointers to pointers
Exercises D1–D5
pointer to a pointer to int, t = p[0];
Exercises D6–D8 while each element of p is a p[0] = p[1];
pointer to int. p[1] = p[2];
p[2] = t;
Subsequently, three int vari- printf("%d %d %d\n",*p[0],*p[1],*p[2]);
ables are allocated, pointed to t = NULL;
by p[0], p[1] & p[2]. free(p[0]);
We show how the ints can be free(p[1]);
read in and, later, how pointers free(p[2]);
to them can be manipulated. free(p);
}
Finally, the allocated variables
and pointers are deallocated us-
ing free().
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 51 / 54
Code Action start program
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
**p = pointer to pointer to int
*t = pointer to int

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action open scope
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
**p = pointer to pointer to int
*t = pointer to int

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action allocate p , t
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p × t×
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
**p = pointer to pointer to int
*t = pointer to int

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action malloc p
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p • t × ×××
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Allocate a block of 3 pointers to int.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action malloc p[0]
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> ?
int main()
{
int **p , *t ;
p • t × • ××
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Allocate an int, pointed to by zeroth element of p.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action malloc p[1]
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> ? ?
int main()
{
int **p , *t ;
p • t × • • ×
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Allocate an int, pointed to by 1st element of p.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action malloc p[2]
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> ? ? ?
int main()
{
int **p , *t ;
p • t × • • •
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Allocate an int, pointed to by 2nd element of p.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action input p[ ];
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
int **p , *t ;
p • t × • • •
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2];
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
The actual variables pointed to by p[0], p[1] & p[2] are *p[0], *p[1] & *p[2], respectively.
To input these, we should put their addresses in scanf thus: scanf(”. . . ”,&*p[0],&*p[1],&*p[2]);
However & = “address of” and * = “item at ” cancel each other.
p[0], p[1] & p[2] are in fact the addresses of the 3 ints.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 52 / 54
Code Action output
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
int **p , *t ;
p • t × • • •
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Print out the 3 ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action t set to p[0]
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
p • t • • • •
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
The pointers are rotated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action p[0] = p[1];
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
p • t • • • •
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
The pointers are rotated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action p[1] = p[2];
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
p • t • • • •
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
The pointers are rotated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action p[2] = t;
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
p • t • • • •
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t;
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
The pointers are rotated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action output
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
p • t • • • •
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Print out the rotated values.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action t set to NULL
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 -2 1
int main()
{
int **p , *t ;
p • t × • • •
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action free(p[0]); p[0]=NULL;
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6 1
int main()
{
int **p , *t ;
p • t × ×• •
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Deallocate the ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action free(p[1]); p[1]=NULL;
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h> 6
int main()
{
int **p , *t ;
p • t × ×× •
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Deallocate the ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action free(p[2]); p[2]=NULL;
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p • t × ×××
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Deallocate the ints.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action free(p) p set to NULL
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p × t×
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}
Deallocate p.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action close scope
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
Code Action END PROGRAM
/*dynamic08.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p , *t ;
p = (int **) malloc (3 *sizeof(int *) ); Stack Memory Dynamic Memory
p[0] = (int *) malloc (1*sizeof(int) );
p[1] = (int *) malloc (1*sizeof(int) );
Input
p[2] = (int *) malloc (1*sizeof(int) );
scanf("%d %d %d",p[0],p[1],p[2]); 6 -2 1
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = p[0]; Output
p[0] = p[1];
p[1] = p[2]; 6 -2 1
p[2] = t; -2 1 6
printf("%d %d %d\n",*p[0],*p[1],*p[2]);
t = NULL;
free(p[0]);
free(p[1]);
free(p[2]);
free(p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises D1–D5

Introduction Exercise D1: Modify dynamic05.c so that it does not use temporary pointers p and
Pointers & Arrays q. The blocks should be accessed using array notation: b[i] = a[i].
Dynamic Memory
Intro-I Exercise D2: Modify dynamic05.c so that it does not use temporary pointers p and
Intro-II q. This time the blocks are accessed using the dereferencing notation *(b+i) etc.
Example
In Action Exercise D3: Modify the solution to Exercise D2, so that b copies over the last 4
malloc() & free()
Memory Leak
elements of a. The output of this program is: a=abracad b=acad
Allocating blocks
Malloc Failure Exercise D4: Explain what would happen in dynamic05.c if the statement:
Run time allocation *q = NULL; were replaced with q = NULL;
Swapping Pointers
Pointers to pointers Exercise D5: The following incomplete program reads in 10 integers into a and
Exercises D1–D5
then counts the number of negative values, c. It then mallocs an array n of size c
Exercises D6–D8
and fills it with only the negative values. Complete this program.
...
#define M 10
int main()
{
int a[M], *n, i, j=0, c=0;
getIntArray(a,M);
...
for(i=0; i<c; i++)
printf("%d ", n[i]);
...
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 53 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises D6–D8

Introduction Exercise D6: Write a program that allocates two character arrays a and b of length
Pointers & Arrays M and reads in two strings of length M-1 into these. It then allocates a third array
Dynamic Memory c and fills it with the elements of a and b in alternating order. For example if M=5
Intro-I and the input strings are WXPQ and abcd, c should be WaXbPcQd. Verify that
Intro-II
Example
your program works by printing out c.
In Action
malloc() & free() Exercise D7: Modify the program of D6 as follows. The input strings can now be
Memory Leak any length between 1 and M-1. The array should contain alternating characters
Allocating blocks from a and b as long as possible, and then the remaining characters from the longer
Malloc Failure
Run time allocation
string. For example, if the inputs are MX and acde, the output should be MaXcde.
Swapping Pointers The array c should not have any unused elements.
Pointers to pointers
Exercises D1–D5 Exercise D8: Write a program that uses pointees to create the structure shown on
Exercises D6–D8 the left. The characters ’a’, ’b’, ’c’ & ’d’ are read in from input. After creating the
structure, the program should print out the char values in the 4 locations. Pointers
should now be changed so as to create the situation on the right and the values
printed out again.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 54

You might also like