You are on page 1of 1038

C Programming:

Memory & Pointers


Part 2: Arrays, Strings & Pointers

Shahid Bokhari

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

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,
Input its included animations and programs, and the associated web site. The programs in this eBook
Arrays
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
Strings programs are not intended to be used as a basis for designing programs for use in the real world.
Pointers 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 / 77
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

Input

Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 3 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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

Input
Try these out now!
Arrays Many pages have content lists on the left.
Strings  Remaining Sections Jump to any topic by clicking on it; return
to the original page using the buttons.
Pointers (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 / 77
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.
Input While there are many fine textbooks that introduce the C
Arrays
language, several important concepts, in particular memory
Strings
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
Pointers
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 / 77
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.
Input While there are many fine textbooks that introduce the C
Arrays
language, several important concepts, in particular memory
Strings
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
Pointers
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 / 77
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.
Input While there are many fine textbooks that introduce the C
Arrays
language, several important concepts, in particular memory
Strings
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
Pointers
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 / 77
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.
Input While there are many fine textbooks that introduce the C
Arrays
language, several important concepts, in particular memory
Strings
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
Pointers
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 / 77
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.
Input While there are many fine textbooks that introduce the C
Arrays
language, several important concepts, in particular memory
Strings
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
Pointers
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 / 77
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
Input
snapshots to represent the progress of a program.
Arrays

Strings This permits the student to carefully study the behavior


Pointers
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 / 77
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
Input
snapshots to represent the progress of a program.
Arrays

Strings This permits the student to carefully study the behavior


Pointers
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 / 77
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
Input
snapshots to represent the progress of a program.
Arrays

Strings This permits the student to carefully study the behavior


Pointers
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 / 77
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
Input
snapshots to represent the progress of a program.
Arrays

Strings This permits the student to carefully study the behavior


Pointers
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 / 77
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
Input
snapshots to represent the progress of a program.
Arrays

Strings This permits the student to carefully study the behavior


Pointers
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 / 77
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
Input
snapshots to represent the progress of a program.
Arrays

Strings This permits the student to carefully study the behavior


Pointers
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 / 77
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

Input

Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 7 / 77
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

Input

Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 7 / 77
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
Input at the solutions.
Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 8 / 77
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
Input at the solutions.
Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 8 / 77
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
Input at the solutions.
Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 8 / 77
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

Input

Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 9 / 77
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.
Input
3. In some situations you may wish to use a smaller win-
Arrays
dow for the eBook so that you can enter, edit, compile
Strings
and execute your own programs using your own edi-
Pointers
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 / 77
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.
Input
3. In some situations you may wish to use a smaller win-
Arrays
dow for the eBook so that you can enter, edit, compile
Strings
and execute your own programs using your own edi-
Pointers
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 / 77
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.
Input
3. In some situations you may wish to use a smaller win-
Arrays
dow for the eBook so that you can enter, edit, compile
Strings
and execute your own programs using your own edi-
Pointers
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 / 77
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.
Input
3. In some situations you may wish to use a smaller win-
Arrays
dow for the eBook so that you can enter, edit, compile
Strings
and execute your own programs using your own edi-
Pointers
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 / 77
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, }
Input there are some aspects that you may not
Arrays
have encountered before, thus it is useful
Strings
to go over the annotations we have pro-
vided.
Pointers
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 / 77
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 }
Input

Arrays As you move through the lines of this program using the space bar, scroll wheel or
Strings the step arrows of eBook, you will see how memory is allocated and the allocated
Pointers 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 / 77
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 }
Input

Arrays The concept of scope is very important. Whenever an opening brace “{” (also
Strings called “curly bracket”) is encountered, a new scope is opened. The eBook indicates
Pointers 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 / 77
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 }
Input

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

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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 }
Input

Arrays Variables are set to specific values.


Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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 }
Input

Arrays Variables are set to specific values.


Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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 }
Input

Arrays Variables are set to specific values.


Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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 }
Input 3 6 7
Arrays These values are output using the printf statement.
Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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 }
Input 3 6 7
Arrays When the closing brace “}” is reached the scope is closed–causing memory to be
Strings deallocated.
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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 }
Input 3 6 7
Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 12 / 77
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

Input It is important for you to be aware of this distinction.


Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 77
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

Input It is important for you to be aware of this distinction.


Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 77
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

Input It is important for you to be aware of this distinction.


Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 77
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

Input It is important for you to be aware of this distinction.


Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 77
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

Input It is important for you to be aware of this distinction.


Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 13 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming Output
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming Output
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming Output
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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);
Input In each case, the outputs before initializa- }
Arrays tion are unpredictable.
Strings
This points out the danger in assuming
anything about the initial values of vari-
Pointers
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 / 77
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

Input

Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Intro
scanf
Functions
Example Function
Sequences
do-while
while
break
Sensing EOF
Signaling EOF Input
Compact solution
Finding Max
Exercises

Arrays

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


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

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Intro

Many interesting issues arise when you wish to input


Introduction information to a program.
Input
Intro You may want to enter information from the keyboard
scanf or from a file.
Functions
Example Function From a keyboard: you may type in whatever you wish
Sequences
after the program starts.
do-while
while
From a file: the simplest way is to use redirection.
break
Sensing EOF
For example, compiling your program as follows:
Signaling EOF
Compact solution cc -o myexec myprog.c
Finding Max creates a file myexec (or, on some systems,
Exercises myexec.exe), which can be executed by typing
Arrays ./myexec (or, on some systems, just myexec).
Strings
Keyboard input can be provided by simply typing in the
Pointers
information after the program starts.
When using redirection, you need to create a file (let us
call it infile) that contains the required input.
The program can then be run as follows:
./myexec < infile (or myexec < infile).
In all cases information must be in the form that the
program expects. If the program expects an integer,
you cannot input a float.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 17 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action start program
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action open scope
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action allocate i
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while i ?
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action allocate x
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while i ? x ?
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action input i
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while i 4 x ?
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action input x
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while i 4 x 2.3
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action output i, x
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while i 4 x 2.3
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

4 2.300000

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action close scope
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

4 2.300000

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action END PROGRAM
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

4 2.300000
Run this program: a typical compilation command is:
cc -o input1 input01.c
Execute by typing in:
./input01
4
2.3
Depending on your system, you may have to type in only input01, not ./input01.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Input with scanf
The scanf function is used to input data to a program.
To input data into a variable i you must use the
Introduction
notation &i, which stands for the “address of i.”
Input The input could come from the keyboard or a file.
Intro
scanf
Code Action END PROGRAM
Functions
Example Function
/* input01.c */
Sequences #include <stdio.h>
do-while int main()
while
break
{
Sensing EOF int i; Memory
Signaling EOF float x;
Compact solution
scanf("%d", &i);
Finding Max Input
Exercises scanf("%f", &x);
Arrays
printf("%d %f\n", i, x); 4
Strings
} 2.3
Pointers
Output

4 2.300000
Alternatively, you can create a file called infile containing the lines:
4
2.3
and the run the program thus:
./input01 < infile (or, input01 < infile)

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 18 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Notes on Functions

Functions are an important part of programming. We will use a


Introduction few functions as necessary, but will not discuss them in detail.
Input
Intro Essentially, a function is a piece of a program that is used many
scanf times in different parts of the program. It is thus convenient to
Functions
package this piece as a separate block and give it a name.
Example Function
Sequences
Whenever this function is required in a program, we simply write
do-while
while its name along with any parameters (i.e. variable names) that
break need to be given to the function.
Sensing EOF
Signaling EOF For example when you use printf(”%d”,x), the parameters are
Compact solution ”%d” and x.
Finding Max
Exercises Functions may be written by you, the user, or may be provided
Arrays by the system.
Strings
You have already used the system provided functions printf() and
Pointers
scanf(). Numerous other functions are provided by the system.
Certain rules must be followed when functions are used or
“called” in C. For example, when you say printf(”%d”,x); your
intention is to print out the value of x. You thus provide the
name of the variable (i.e. x) that you wish to print out.
When you use scanf, your intention is to read in a value into a
location &x; you therefore say scanf(”%d”,&x); here &x is the
address of the location where you want the value to go.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 19 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Example Function

/* func01.c */
As an example of a user written function,
#include <stdio.h>
Introduction consider max, shown opposite.
Input
Intro This function takes a and b, computes their int max(int a, int b)
scanf maximum and returns the answer. {
Functions
if (a>b)
Example Function When this function is called, the values x and return a;
Sequences
y in the main program replace the variables else
do-while
while a and b in the function.
return b;
break
Sensing EOF The returned value is copied into z in the }
Signaling EOF main program.
Compact solution int main()
Finding Max
{
Exercises
int x, y, z;
Arrays
scanf("%d",&x);
Strings
scanf("%d",&y);
Pointers
z = max(x,y);
printf("%d\n",z);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Example Function

/* func01.c */
As an example of a user written function,
#include <stdio.h>
Introduction consider max, shown opposite.
Input
Intro This function takes a and b, computes their int max(int a, int b)
scanf maximum and returns the answer. {
Functions
if (a>b)
Example Function When this function is called, the values x and return a;
Sequences
y in the main program replace the variables else
do-while
while a and b in the function.
return b;
break
Sensing EOF The returned value is copied into z in the }
Signaling EOF main program.
Compact solution int main()
Finding Max
{
Exercises
int x, y, z;
Arrays
scanf("%d",&x);
Strings
scanf("%d",&y);
Pointers
z = max(x,y);
printf("%d\n",z);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Example Function

/* func01.c */
As an example of a user written function,
#include <stdio.h>
Introduction consider max, shown opposite.
Input
Intro This function takes a and b, computes their int max(int a, int b)
scanf maximum and returns the answer. {
Functions
if (a>b)
Example Function When this function is called, the values x and return a;
Sequences
y in the main program replace the variables else
do-while
while a and b in the function.
return b;
break
Sensing EOF The returned value is copied into z in the }
Signaling EOF main program.
Compact solution int main()
Finding Max
{
Exercises
int x, y, z;
Arrays
scanf("%d",&x);
Strings
scanf("%d",&y);
Pointers
z = max(x,y);
printf("%d\n",z);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Example Function

/* func01.c */
As an example of a user written function,
#include <stdio.h>
Introduction consider max, shown opposite.
Input
Intro This function takes a and b, computes their int max(int a, int b)
scanf maximum and returns the answer. {
Functions
if (a>b)
Example Function When this function is called, the values x and return a;
Sequences
y in the main program replace the variables else
do-while
while a and b in the function.
return b;
break
Sensing EOF The returned value is copied into z in the }
Signaling EOF main program.
Compact solution int main()
Finding Max
{
Exercises
int x, y, z;
Arrays
scanf("%d",&x);
Strings
scanf("%d",&y);
Pointers
z = max(x,y);
printf("%d\n",z);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 20 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Inputting a sequence of data

Introduction /* input02.c */
There is often the need to input a se-
Input #include <stdio.h>
Intro quence of data to a program.
int main()
scanf
Functions The next few programs will develop {
Example Function several solutions to this problem. int i;
Sequences do
do-while The program on this page reads in a
while
{
sequence of integer values and prints int s;
break
out each number and its square.
Sensing EOF scanf("%d", &i);
Signaling EOF
Compact solution
A do-while loop is used that terminates s = i*i;
Finding Max when a zero is read in. printf("%2d %4d\n", i,s);
Exercises }
Arrays while(i != 0);
Strings }
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Inputting a sequence of data

Introduction /* input02.c */
There is often the need to input a se-
Input #include <stdio.h>
Intro quence of data to a program.
int main()
scanf
Functions The next few programs will develop {
Example Function several solutions to this problem. int i;
Sequences do
do-while The program on this page reads in a
while
{
sequence of integer values and prints int s;
break
out each number and its square.
Sensing EOF scanf("%d", &i);
Signaling EOF
Compact solution
A do-while loop is used that terminates s = i*i;
Finding Max when a zero is read in. printf("%2d %4d\n", i,s);
Exercises }
Arrays while(i != 0);
Strings }
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Inputting a sequence of data

Introduction /* input02.c */
There is often the need to input a se-
Input #include <stdio.h>
Intro quence of data to a program.
int main()
scanf
Functions The next few programs will develop {
Example Function several solutions to this problem. int i;
Sequences do
do-while The program on this page reads in a
while
{
sequence of integer values and prints int s;
break
out each number and its square.
Sensing EOF scanf("%d", &i);
Signaling EOF
Compact solution
A do-while loop is used that terminates s = i*i;
Finding Max when a zero is read in. printf("%2d %4d\n", i,s);
Exercises }
Arrays while(i != 0);
Strings }
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Inputting a sequence of data

Introduction /* input02.c */
There is often the need to input a se-
Input #include <stdio.h>
Intro quence of data to a program.
int main()
scanf
Functions The next few programs will develop {
Example Function several solutions to this problem. int i;
Sequences do
do-while The program on this page reads in a
while
{
sequence of integer values and prints int s;
break
out each number and its square.
Sensing EOF scanf("%d", &i);
Signaling EOF
Compact solution
A do-while loop is used that terminates s = i*i;
Finding Max when a zero is read in. printf("%2d %4d\n", i,s);
Exercises }
Arrays while(i != 0);
Strings }
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 21 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action start program


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action open scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action allocate i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i ?
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action open scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i ?
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action allocate s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i ?
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action input i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action s set to i*i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 64
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action output i,s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 64
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action close scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action while(i 6= 0);


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action open scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action allocate s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i 8
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action input i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action s set to i*i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 9
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action output i,s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 9
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action close scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action while(i 6= 0);


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action open scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action allocate s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i -3
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action input i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action s set to i*i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 121
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action output i,s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 121
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action close scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action while(i 6= 0);


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action open scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action allocate s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i 11
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action input i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s ?
Functions {
Example Function int i;
Sequences i 0
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action s set to i*i


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 0
Functions {
Example Function int i;
Sequences i 0
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121

The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action output i,s


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main() s 0
Functions {
Example Function int i;
Sequences i 0
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121
0 0
The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action close scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 0
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121
0 0
The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action while(i 6= 0);


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences i 0
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121
0 0
The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action close scope


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121
0 0
The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
do-while Loop

Code Action END PROGRAM


Introduction
/* input02.c */
Input
#include <stdio.h>
Intro
scanf
int main()
Functions {
Example Function int i;
Sequences
do-while
do
while { Memory
break int s;
Sensing EOF
scanf("%d", &i);
Signaling EOF Input
Compact solution s = i*i;
Finding Max printf("%2d %4d\n", i,s); 8
Exercises } -3
Arrays while(i != 0); 11
Strings } 0
Pointers
Output

8 64
-3 9
11 121
0 0
The zero that signals the end of the input sequence is also called a sentinel, which
can be any predetermined value–not just 0.
This loop is awkward because the sentinel and its square are also printed.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 22 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action start program
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
while
int i;
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action open scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
while
int i;
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action allocate i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i ?
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action input i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 8
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action while(i 6= 0)
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 8
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action open scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 8
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action allocate s
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s ?
Sequences
{
do-while
int i;
while i 8
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action s set to i*i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 64
Sequences
{
do-while
int i;
while i 8
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action output i, s
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 64
Sequences
{
do-while
int i;
while i 8
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action input i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 64
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action close scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action while(i 6= 0)
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action open scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action allocate s
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s ?
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action s set to i*i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 9
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action output i, s
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 9
Sequences
{
do-while
int i;
while i -3
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action input i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 9
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action close scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action while(i 6= 0)
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action open scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action allocate s
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s ?
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action s set to i*i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 121
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action output i, s
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 121
Sequences
{
do-while
int i;
while i 11
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action input i
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main() s 121
Sequences
{
do-while
int i;
while i 0
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action close scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 0
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action while(i 6= 0)
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
int i;
while i 0
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action close scope
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
while
int i;
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
while Loop

We can replace the do-while with a while to eliminate printing of the sentinel.
Introduction
However, this requires an extra scanf.
Input Code Action END PROGRAM
Intro
scanf
/* input03.c */
Functions #include <stdio.h>
Example Function int main()
Sequences
{
do-while
while
int i;
break scanf("%d", &i);
Sensing EOF while(i != 0) Memory
Signaling EOF
Compact solution
{
Finding Max int s;
Input
Exercises s = i*i;
Arrays printf("%2d %4d\n", i, s); 8
Strings scanf("%d", &i); -3
Pointers } 11
} 0

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 23 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action start program
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action open scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action open scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action allocate i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i ?
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action input i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 8
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action if (i equals 0)
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 8
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action output i,i*i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 8
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action close scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action open scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action allocate i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i ?
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action input i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i -3
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action if (i equals 0)
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i -3
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action output i,i*i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i -3
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action close scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action open scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action allocate i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i ?
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action input i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 11
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action if (i equals 0)
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 11
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9

No variables are declared in the outer scope–the frame representing it is therefore


empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action output i,i*i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 11
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
No variables are declared in the outer scope–the frame representing it is therefore
empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action close scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
No variables are declared in the outer scope–the frame representing it is therefore
empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action open scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
No variables are declared in the outer scope–the frame representing it is therefore
empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action allocate i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i ?
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
No variables are declared in the outer scope–the frame representing it is therefore
empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action input i
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 0
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
No variables are declared in the outer scope–the frame representing it is therefore
empty.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action if (i equals 0)
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main() i 0
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
When the break statement executes, the inner scope is closed and the loop
terminates.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action break;
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
When the break statement executes, the inner scope is closed and the loop
terminates.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action close scope
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
When the break statement executes, the inner scope is closed and the loop
terminates.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Infinite Loops and breaks

The while(1) in this program is always true–this is an infinite loop and requires a
Introduction break statement for termination.
Input Code Action END PROGRAM
Intro /* input04.c */
scanf
Functions
#include <stdio.h>
Example Function int main()
Sequences {
do-while
while
while(1)
break {
Sensing EOF int i; Memory
Signaling EOF
scanf("%d", &i);
Compact solution
Finding Max if (i == 0)
Input
Exercises break;
Arrays printf("%2d %4d\n", i,i*i); 8
Strings } -3
Pointers
} 11
0

Output

8 64
-3 9
11 121
When the break statement executes, the inner scope is closed and the loop
terminates.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 24 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action start program
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action open scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action open scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action allocate i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i ?
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action input
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i 8
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action output i,i*i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i 8
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action close scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action open scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action allocate i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i ?
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action input
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i -3
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action output i,i*i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i -3
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action close scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action open scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action allocate i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i ?
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action input
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i 11
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action output i,i*i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i 11
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action close scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action open scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action allocate i
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i ?
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action input
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main() i ?
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121
At this point EOF is true and the value of i is undefined.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action break;
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121
The break is executed, inner scope closed and loop terminated.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action close scope
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Sensing the End-of-File

It is desirable to be able to dispense with the sentinel and run the program on
Introduction
however many items there are in a file.
Input
This requires us to be able to sense the end of a file, a facility conveniently provided
Intro by the scanf function as shown below.
scanf
Functions Code Action END PROGRAM
Example Function /* input05.c */
Sequences
#include <stdio.h>
do-while
while
int main()
break {
Sensing EOF while(1)
Signaling EOF
Compact solution
{
Finding Max int i; Memory
Exercises if(scanf("%d", &i) == EOF)
Arrays break;
Input
Strings printf("%2d %4d\n", i,i*i);
Pointers } 8
} -3
11

Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 25 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Signaling the End-of-File
Running a program which senses the End-of-File is simple if input is from a file.
Introduction On Unix, Linux, Solaris, etc. if you compile your program using a command like
Input cc -o input5 input5.c, and your data file is called infile, you would run it
Intro thus: ./input5 < infile
scanf
Functions On a Windows system, the executable would typically be called input5.exe and
Example Function
you would execute it thus: input5 < infile
Sequences
do-while
However, you may want to test this program using input from the keyboard,
while
break rather than from an input file. In this case there is an important difference
Sensing EOF between Windows and other systems.
Signaling EOF
Compact solution Running on Unix, Linux, Solaris etc., you would terminate your keyboard input
Finding Max with Ctrl-d (i.e. hold down the Ctrl key and press the d key):
Exercises
./input
Arrays
8
Strings -3
Pointers 11
Ctrl-d
On a Windows system, you would terminate your keyboard input with Ctrl-z,
Enter (i.e. hold down the Ctrl key and press the z key, then press the Enter key):
input
8
-3
11
Ctrl-z Enter

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Signaling the End-of-File
Running a program which senses the End-of-File is simple if input is from a file.
Introduction On Unix, Linux, Solaris, etc. if you compile your program using a command like
Input cc -o input5 input5.c, and your data file is called infile, you would run it
Intro thus: ./input5 < infile
scanf
Functions On a Windows system, the executable would typically be called input5.exe and
Example Function
you would execute it thus: input5 < infile
Sequences
do-while
However, you may want to test this program using input from the keyboard,
while
break rather than from an input file. In this case there is an important difference
Sensing EOF between Windows and other systems.
Signaling EOF
Compact solution Running on Unix, Linux, Solaris etc., you would terminate your keyboard input
Finding Max with Ctrl-d (i.e. hold down the Ctrl key and press the d key):
Exercises
./input
Arrays
8
Strings -3
Pointers 11
Ctrl-d
On a Windows system, you would terminate your keyboard input with Ctrl-z,
Enter (i.e. hold down the Ctrl key and press the z key, then press the Enter key):
input
8
-3
11
Ctrl-z Enter

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Signaling the End-of-File
Running a program which senses the End-of-File is simple if input is from a file.
Introduction On Unix, Linux, Solaris, etc. if you compile your program using a command like
Input cc -o input5 input5.c, and your data file is called infile, you would run it
Intro thus: ./input5 < infile
scanf
Functions On a Windows system, the executable would typically be called input5.exe and
Example Function
you would execute it thus: input5 < infile
Sequences
do-while
However, you may want to test this program using input from the keyboard,
while
break rather than from an input file. In this case there is an important difference
Sensing EOF between Windows and other systems.
Signaling EOF
Compact solution Running on Unix, Linux, Solaris etc., you would terminate your keyboard input
Finding Max with Ctrl-d (i.e. hold down the Ctrl key and press the d key):
Exercises
./input
Arrays
8
Strings -3
Pointers 11
Ctrl-d
On a Windows system, you would terminate your keyboard input with Ctrl-z,
Enter (i.e. hold down the Ctrl key and press the z key, then press the Enter key):
input
8
-3
11
Ctrl-z Enter

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Signaling the End-of-File
Running a program which senses the End-of-File is simple if input is from a file.
Introduction On Unix, Linux, Solaris, etc. if you compile your program using a command like
Input cc -o input5 input5.c, and your data file is called infile, you would run it
Intro thus: ./input5 < infile
scanf
Functions On a Windows system, the executable would typically be called input5.exe and
Example Function
you would execute it thus: input5 < infile
Sequences
do-while
However, you may want to test this program using input from the keyboard,
while
break rather than from an input file. In this case there is an important difference
Sensing EOF between Windows and other systems.
Signaling EOF
Compact solution Running on Unix, Linux, Solaris etc., you would terminate your keyboard input
Finding Max with Ctrl-d (i.e. hold down the Ctrl key and press the d key):
Exercises
./input
Arrays
8
Strings -3
Pointers 11
Ctrl-d
On a Windows system, you would terminate your keyboard input with Ctrl-z,
Enter (i.e. hold down the Ctrl key and press the z key, then press the Enter key):
input
8
-3
11
Ctrl-z Enter

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Signaling the End-of-File
Running a program which senses the End-of-File is simple if input is from a file.
Introduction On Unix, Linux, Solaris, etc. if you compile your program using a command like
Input cc -o input5 input5.c, and your data file is called infile, you would run it
Intro thus: ./input5 < infile
scanf
Functions On a Windows system, the executable would typically be called input5.exe and
Example Function
you would execute it thus: input5 < infile
Sequences
do-while
However, you may want to test this program using input from the keyboard,
while
break rather than from an input file. In this case there is an important difference
Sensing EOF between Windows and other systems.
Signaling EOF
Compact solution Running on Unix, Linux, Solaris etc., you would terminate your keyboard input
Finding Max with Ctrl-d (i.e. hold down the Ctrl key and press the d key):
Exercises
./input
Arrays
8
Strings -3
Pointers 11
Ctrl-d
On a Windows system, you would terminate your keyboard input with Ctrl-z,
Enter (i.e. hold down the Ctrl key and press the z key, then press the Enter key):
input
8
-3
11
Ctrl-z Enter

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Signaling the End-of-File
Running a program which senses the End-of-File is simple if input is from a file.
Introduction On Unix, Linux, Solaris, etc. if you compile your program using a command like
Input cc -o input5 input5.c, and your data file is called infile, you would run it
Intro thus: ./input5 < infile
scanf
Functions On a Windows system, the executable would typically be called input5.exe and
Example Function
you would execute it thus: input5 < infile
Sequences
do-while
However, you may want to test this program using input from the keyboard,
while
break rather than from an input file. In this case there is an important difference
Sensing EOF between Windows and other systems.
Signaling EOF
Compact solution Running on Unix, Linux, Solaris etc., you would terminate your keyboard input
Finding Max with Ctrl-d (i.e. hold down the Ctrl key and press the d key):
Exercises
./input
Arrays
8
Strings -3
Pointers 11
Ctrl-d
On a Windows system, you would terminate your keyboard input with Ctrl-z,
Enter (i.e. hold down the Ctrl key and press the z key, then press the Enter key):
input
8
-3
11
Ctrl-z Enter

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 26 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action start program
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main()
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action open scope
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main()
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action allocate i
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i ?
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action input
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i 8
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action output i,i*i
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i 8
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action input
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i -3
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action output i,i*i
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i -3
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action input
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i 11
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64
-3 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action output i,i*i
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i 11
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64
-3 9
11 121

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action input
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main() i 11
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64
-3 9
11 121
No more input: EOF is true. i retains the previous value. Loop terminated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action close scope
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main()
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64
-3 9
11 121
No more input: EOF is true. i retains the previous value. Loop terminated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Compact Solution

In this short but effective program the while statement contains the scanf.
Introduction
The scanf is executed as long as it does not return EOF, which achieves our
Input
objective.
Intro
scanf
Code Action END PROGRAM
Functions /* input06.c */
Example Function #include <stdio.h>
Sequences
int main()
do-while
while
{
break int i; Memory
Sensing EOF while((scanf("%d", &i) != EOF))
Signaling EOF
printf("%2d %4d\n", i,i*i);
Compact solution Input
Finding Max }
Exercises
8
Arrays -3
Strings 11
Pointers
Output

8 64
-3 9
11 121
No more input: EOF is true. i retains the previous value. Loop terminated.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 27 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action start program
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action allocate i, m
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i ? m ?
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 3 m ?
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action m set to i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 3 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 3 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 3 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action if( i > m)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 3 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 7 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 7 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 7 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 7 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action if( i > m)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 7 m 3
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action m set to i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 7 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 2 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 2 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 2 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 2 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action if( i > m)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 2 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 0 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 0 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 0 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 0 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action if( i > m)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 0 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 9 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 9 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 9 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 9 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action if( i > m)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 9 m 7
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action m set to i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 9 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 4 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 4 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 4 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action open scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 4 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action if( i > m)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i 4 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action input i
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i -9 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i -9 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action while(i 6= -9)
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i -9 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action output m
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF i -9 m 9
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

the max is 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action close scope
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

the max is 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 28 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Finding Maximum

The following program inputs a sequence of integers terminated by a sentinel (-9)


Introduction and prints out the maximum value. It is assumed that the input file has at least one
Input
number in it (followed by the sentinel).
Intro
scanf
Code Action END PROGRAM
Functions /* input07.c */
Example Function #include <stdio.h>
Sequences
do-while
int main()
while {
break int i, m;
Sensing EOF
scanf("%d", &i);
Signaling EOF
Compact solution
m = i; Memory
Finding Max while(i != -9)
Exercises {
Input
Arrays if( i > m)
Strings m = i; 3
Pointers scanf("%d", &i); 7
} 2
printf("the max is %d\n",m); 0
} 9
4
-9

Output

the max is 9

c 2008 Algopath LLC. All Rights Reserved.


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

Exercise I1 Modify input05.c so that it computes the sum of all the numbers in
Introduction
the input.
Input Exercise I2 Further modify the above program to print out the total number of
Intro values summed as well.
scanf
Functions Exercise I3 Write a program that will print out the line numbers of the input file
Example Function that have negative numbers in them. For example, if you input the file:
Sequences
do-while
3
while -4
break 4
Sensing EOF
-2
Signaling EOF
Compact solution 8
Finding Max 9
Exercises -3
Arrays it should output:
Strings 2
Pointers 4
7
Exercise I4 Modify input07.c so that it prints out both the minimum and the
maximum values in the input.
Exercise I5 Rewrite input07.c so that it does not use a sentinel but rather senses
for EOF. The program should print out both minimum and maximum and work for
input files of any length. It should print out the message “empty file” if the input
file has no numbers in it.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 29 / 77
C Programming:
Memory & Pointers
An Algopath eBook

Introduction

Input

Arrays
Intro
Access
Input
Array I/O
Output
Sum
Odd Sum
Copy
Reverse
Arrays
Flip
Odd Flip
Float arrays
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 30 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Arrays

/* array01.c */
In many situations we need to refer to a sequence
#include <stdio.h>
Introduction or series of variables with a single name.
Input
int main()
Arrays provide a powerful mechanism for manipu- {
Arrays
Intro lating such sequences. int i, j, a[5];
Access a[3] = -2;
Input An int array a with 5 elements is declared in this a[0] = 3;
Array I/O program. a[4] = 4;
Output
Sum The elements of this array are a[0], a[1], a[2], a[3] i = 2; j = 0;
Odd Sum and a[4]. a[i] = 9;
Copy
a[3] = a[i]*a[j];
Reverse The elements of the array can be used in any way a[1] = a[2]-11;
Flip
Odd Flip
that an ordinary int variable is used. a[3] = a[3]/a[i*2];
Float arrays
In addition to int we can have arrays of type float, }
Overrunning
Memory layout char, etc.
Bounds Errors
Char arrays Remember always that an array declared: int a[5],
Overrunning chars has 5 elements, a[0] to a[4]. There is no element
Scopes
a[5].
Exercises

Strings In the C programming language, there is no mech-


Pointers anism preventing you from trying to access array
elements out of the bounds you have declared.
These illegal accesses can have unpleasant or even
dangerous consequences, as we will see later in this
Section.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 31 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action start program
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action open scope
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action allocate i, j, a[5]
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i ? j ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[3] = -2;
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i ? j ? a[0] ? a[1] ? a[2] ? a[3] -2 a[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[0] = 3;
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i ? j ? a[0] 3 a[1] ? a[2] ? a[3] -2 a[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[4] = 4;
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i ? j ? a[0] 3 a[1] ? a[2] ? a[3] -2 a[4] 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action set variables
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i 2 j 0 a[0] 3 a[1] ? a[2] ? a[3] -2 a[4] 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[i] = 9;
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i 2 j 0 a[0] 3 a[1] ? a[2] 9 a[3] -2 a[4] 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[3] = a[i]*a[j];
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i 2 j 0 a[0] 3 a[1] ? a[2] 9 a[3] 27 a[4] 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[1] = a[2]-11;
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i 2 j 0 a[0] 3 a[1] -2 a[2] 9 a[3] 27 a[4] 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action a[3] = a[3]/a[i*2];
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

i 2 j 0 a[0] 3 a[1] -2 a[2] 9 a[3] 6 a[4] 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action close scope
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Accessing Array Elements

The program introduced on the previous page is now executed. An array variable
Introduction
can be used just like an ordinary variable.
Input
The only difference is that we can refer to it with an index. This index can be a
constant or a variable.
Arrays
Intro
The declaration:int a[5], means “allocate an array called a with 5 elements.”
Access The expression a[3] = -2 means “set the element of a indexed 3 to -2.”
Input
Array I/O Code Action END PROGRAM
Output /* array01.c */
Sum
Odd Sum
#include <stdio.h>
Copy int main()
Reverse {
Flip
Odd Flip
int i, j, a[5];
Float arrays a[3] = -2;
Overrunning a[0] = 3;
Memory layout
a[4] = 4;
Bounds Errors
Char arrays
i = 2; j = 0;
Overrunning chars a[i] = 9;
Scopes a[3] = a[i]*a[j];
Exercises
a[1] = a[2]-11;
Strings
a[3] = a[3]/a[i*2];
Pointers }

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 32 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action start program
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action open scope
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action allocate i, a[5]
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action for loop
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 0 a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action input a[i]
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 0 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action for loop
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 1 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action input a[i]
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 1 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action for loop
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 2 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action input a[i]
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 2 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action for loop
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 3 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action input a[i]
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action for loop
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action input a[i]
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action for loop
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action close scope
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input

A convenient and compact way to input data to an array is to use scanf inside a
Introduction
loop.
Input
In the following, a for loop makes i go from 0 to 4, while reading in a[i].
Note that & is used in the scanf, just as it would be with an ordinary variable.
Arrays
Intro Code Action END PROGRAM
Access
Input
/* array02.c */
Array I/O #include <stdio.h>
Output #define M 5 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 33 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action start program
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action open scope
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action allocate i, a[5]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 0 a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action input a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 0 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 1 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action input a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 1 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 2 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action input a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 2 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 3 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action input a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ?
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action input a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays }
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 0 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action output a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 0 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action output a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises
i 2 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action output a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays
Overrunning chars
Scopes
Exercises
i 2 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays
Overrunning chars
Scopes
Exercises
i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action output a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays -2
Overrunning chars
Scopes
Exercises
i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays -2
Overrunning chars
Scopes
Exercises
i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action output a[i]
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays -2
Overrunning chars 4
Scopes
Exercises
i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action for loop
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays -2
Overrunning chars 4
Scopes
Exercises
i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4
Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action close scope
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays -2
Overrunning chars 4
Scopes
Exercises

Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Input/Output

Loops are also used for array output, as shown below.


Introduction Code Action END PROGRAM
Input /* array03.c */
Arrays #include <stdio.h>
Intro #define M 5 Input
Access
Input
int main() 3
Array I/O { 2
Output int i, a[M]; 9
Sum
Odd Sum
for(i=0; i<M; i++) -2
Copy scanf("%d",&a[i]); 4
Reverse for(i=0; i<M; i++)
Flip
printf("%d\n",a[i]); Output
Odd Flip
Float arrays } 3
Overrunning 2
Memory layout
Bounds Errors
9
Char arrays -2
Overrunning chars 4
Scopes
Exercises

Strings

Pointers Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 34 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action start program
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action open scope
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action allocate i, a[5]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 0 a[0] ? a[1] ? a[2] ? a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action input a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 0 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 1 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action input a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 1 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action input a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action input a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action input a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action output a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action output a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays
Overrunning chars
Scopes
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action output a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars
Scopes
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars
Scopes
Exercises

Strings i 1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action output a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars 2
Scopes
Exercises

Strings i 1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars 2
Scopes
Exercises

Strings i 0 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action output a[i]
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars 2
Scopes
3
Exercises

Strings i 0 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action for loop
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars 2
Scopes
3
Exercises

Strings i -1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action close scope
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars 2
Scopes
3
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array Output

As array elements can be accessed in any order, the loop controlling output can be
Introduction
made to print them in reverse order, as shown.
Input Code Action END PROGRAM
Arrays /* array04.c */
Intro #include <stdio.h>
Access
#define M 5 Input
Input
Array I/O int main() 3
Output { 2
Sum
Odd Sum
int i, a[M]; 9
Copy for(i=0; i<M; i++) -2
Reverse scanf("%d",&a[i]); 4
Flip
for(i=M -1; i>=0; i-- )
Odd Flip
Float arrays printf("%d\n",a[i]); Output
Overrunning } 4
Memory layout
Bounds Errors
-2
Char arrays 9
Overrunning chars 2
Scopes
3
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 35 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action start program
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action open scope
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action allocate i, s, a[6]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 0 a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action input a[i]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 0 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 1 a[0] 3 a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action input a[i]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 1 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] ? a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action input a[i]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action input a[i]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] ? a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action input a[i]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] ? s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action input a[i]
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s ?


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

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


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s set to 0
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

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


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 0 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 0


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s += a[i];
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 0 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 3


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 3


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s += a[i];
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 1 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 5


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 5


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s += a[i];
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 2 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 14


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 14


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s += a[i];
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 3 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 12


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 12


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s += a[i];
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 4 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 16


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 16


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action s += a[i];
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 5 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 17


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action for loop
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s);
Scopes }
Exercises

Strings i 6 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 17


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action output s)
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s); sum = 17
Scopes }
Exercises

Strings i 6 a[0] 3 a[1] 2 a[2] 9 a[3] -2 a[4] 4 a[5] 1 s 17


Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action close scope
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s); sum = 17
Scopes }
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Elements

Loops are also used to step through arrays in order to compute something based on
Introduction
the data stored in the array.
Input
In the following we compute the sum of all elements in the array.
We could similarly compute the max or min of the elements in the array.
Arrays
Intro Code Action END PROGRAM
Access
Input
/* array05.c */
Array I/O #include <stdio.h>
Output #define M 6 Input
Sum
Odd Sum
int main() 3
Copy { 2
Reverse int i, s, a[M]; 9
Flip
for(i=0; i<M; i++) -2
Odd Flip
Float arrays scanf("%d",&a[i]); 4
Overrunning s = 0; 1
Memory layout for(i=0; i<M; i++)
Bounds Errors
s += a[i]; Output
Char arrays
Overrunning chars printf("sum = %d\n",s); sum = 17
Scopes }
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 36 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action start program
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action open scope
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action allocate i, s, a[6]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action input a[i]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action input a[i]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action input a[i]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

i 2 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ? a[5] ? s ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

i 3 a[0] 3 a[1] 2 a[2] 9 a[3] ? a[4] ? a[5] ? s ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action input a[i]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action input a[i]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action input a[i]
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action s set to 0
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action if(i%2 equals 1)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action if(i%2 equals 1)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action s += a[i];
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action if(i%2 equals 1)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action if(i%2 equals 1)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action s += a[i];
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action if(i%2 equals 1)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action if(i%2 equals 1)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action s += a[i];
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action for loop
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i];
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action output s)
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i]; odd sum = 1
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

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

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action close scope
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i]; odd sum = 1
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Summing Odd-Indexed Elements

A slightly more complicated example is the sum of all odd elements.


Introduction
Here we only add up the elements whose index is odd.
Input
We can establish if a number i is odd by checking if the remainder obtained when i
is divided by 2 is 1.
Arrays
Intro
This check is in the expression if (i % 2 == 1) .
Access
Input
Code Action END PROGRAM
Array I/O /* array06.c */
Output #include <stdio.h>
Sum
#define N 6 Input
Odd Sum
Copy int main() 3
Reverse { 2
Flip
int i, s, a[N]; 9
Odd Flip
Float arrays for(i=0; i<N; i++) -2
Overrunning scanf("%d",&a[i]); 4
Memory layout s = 0; 1
Bounds Errors
Char arrays
for(i=0; i<N; i++)
Overrunning chars if(i%2 == 1) Output
Scopes s += a[i]; odd sum = 1
Exercises
printf("odd sum = %d\n",s);
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 37 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action start program
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action open scope
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action allocate i, a[4], b[4]
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i ? a[0] ? a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 0 a[0] ? a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action a[i] = i*3;
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 0 a[0] 0 a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 1 a[0] 0 a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action a[i] = i*3;
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 1 a[0] 0 a[1] 3 a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 2 a[0] 0 a[1] 3 a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action a[i] = i*3;
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 2 a[0] 0 a[1] 3 a[2] 6 a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 3 a[0] 0 a[1] 3 a[2] 6 a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action a[i] = i*3;
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 3 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 4 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 0 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] ? b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action b[i] = a[i];
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 0 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 1 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] ? b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action b[i] = a[i];
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 1 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] 3 b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 2 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] 3 b[2] ? b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action b[i] = a[i];
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 2 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] 3 b[2] 6 b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 3 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] 3 b[2] 6 b[3] ?
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action b[i] = a[i];
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 3 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] 3 b[2] 6 b[3] 9
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action for loop
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
i 4 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 0 b[1] 3 b[2] 6 b[3] 9
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action close scope
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Copying an Array

For purposes of illustration, we first set element a[i] to 3*i.


Introduction a is then copied into b using a simple for loop.
Input Code Action END PROGRAM
Arrays /* array07.c */
Intro
#include <stdio.h>
Access
Input #define M 4
Array I/O int main()
Output {
Sum
Odd Sum
int i, a[M], b[M];
Copy for(i=0; i<M; i++)
Reverse a[i] = i*3;
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays b[i] = a[i];
Overrunning }
Memory layout
Bounds Errors
Char arrays
Overrunning chars
Scopes Memory
Exercises

Strings

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 38 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action start program
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action open scope
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action allocate i, a[4], b[4]
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i ? a[0] ? a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 0 a[0] ? a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action a[i] = i*3;
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 0 a[0] 0 a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 1 a[0] 0 a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action a[i] = i*3;
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 1 a[0] 0 a[1] 3 a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 2 a[0] 0 a[1] 3 a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action a[i] = i*3;
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 2 a[0] 0 a[1] 3 a[2] 6 a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 3 a[0] 0 a[1] 3 a[2] 6 a[3] ? b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action a[i] = i*3;
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 3 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 4 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 0 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] ? b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action b[i] = a[4-1-i];
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 0 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 1 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] ? b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action b[i] = a[4-1-i];
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 1 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] 6 b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 2 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] 6 b[2] ? b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action b[i] = a[4-1-i];
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 2 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] 6 b[2] 3 b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 3 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] 6 b[2] 3 b[3] ?
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action b[i] = a[4-1-i];
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 3 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] 6 b[2] 3 b[3] 0
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action for loop
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes i 4 a[0] 0 a[1] 3 a[2] 6 a[3] 9 b[0] 9 b[1] 6 b[2] 3 b[3] 0
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action close scope
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse Copy

A simple for loop is used to copy a to b in reverse order.


Introduction
In this program, the challenge is to compute the reverse indexes correctly.
Input
The index of a must start at the last element, M − 1 = 3 (one less than the size of
the array), and go down to 0.
Arrays
Intro
Code Action END PROGRAM
Access
Input /* array08.c */
Array I/O #include <stdio.h>
Output #define M 4
Sum
Odd Sum
int main()
Copy {
Reverse int i, a[M], b[M];
Flip
Odd Flip
for(i=0; i<M; i++)
Float arrays a[i] = i*3;
Overrunning for(i=0; i<M; i++)
Memory layout
b[i] = a[M-1-i];
Bounds Errors
Char arrays
}
Overrunning chars
Scopes
Exercises

Strings Memory
Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 39 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action start program
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action open scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action allocate i, a[6]
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 0 a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action a[i] = i;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 0 a[0] 0 a[1] ? a[2] ? a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 1 a[0] 0 a[1] ? a[2] ? a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action a[i] = i;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 1 a[0] 0 a[1] 1 a[2] ? a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 2 a[0] 0 a[1] 1 a[2] ? a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action a[i] = i;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 2 a[0] 0 a[1] 1 a[2] 2 a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 3 a[0] 0 a[1] 1 a[2] 2 a[3] ? a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action a[i] = i;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 3 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 4 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] ? a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action a[i] = i;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 4 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 5 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action a[i] = i;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 5 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

An in place reversal is done without copying the entire array into another.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 6 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

Exchange elements at the ends of array: repeat at increasing distances from ends.
Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

Exchange elements at the ends of array: repeat at increasing distances from ends.
Introduction Code Action open scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

Exchange elements at the ends of array: repeat at increasing distances from ends.
Introduction Code Action allocate t
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t ?
Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action t set to a[i]
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 0
Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action a[i] = a[6-1-i];
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 0
Pointers
i 0 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action a[6-1-i] = t;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 0
Pointers
i 0 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action close scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 0 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action open scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action allocate t
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t ?
Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action t set to a[i]
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 1
Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action a[i] = a[6-1-i];
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 1
Pointers
i 1 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 4 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action a[6-1-i] = t;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 1
Pointers
i 1 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action close scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 1 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action open scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action allocate t
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t ?
Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action t set to a[i]
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 2
Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action a[i] = a[6-1-i];
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 2
Pointers
i 2 a[0] 5 a[1] 4 a[2] 3 a[3] 3 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action a[6-1-i] = t;
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings t 2
Pointers
i 2 a[0] 5 a[1] 4 a[2] 3 a[3] 2 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action close scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 2 a[0] 5 a[1] 4 a[2] 3 a[3] 2 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action for loop
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers
i 3 a[0] 5 a[1] 4 a[2] 3 a[3] 2 a[4] 1 a[5] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action close scope
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place

A temporary location t is required to hold a value while a swap is made.


Introduction Code Action END PROGRAM
Input /* array09.c */
Arrays #include <stdio.h>
Intro #define P 6
Access
Input
int main()
Array I/O {
Output int i, a[P];
Sum
Odd Sum
for(i=0; i<P; i++)
Copy a[i] = i;
Reverse for(i=0; i<P/2 ; i++)
Flip
{
Odd Flip
Float arrays int t;
Overrunning t = a[i];
Memory layout a[i] = a[P-1-i];
Bounds Errors
Char arrays
a[P-1-i] = t;
Overrunning chars }
Scopes }
Exercises

Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 40 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action start program
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action open scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action allocate i, a[7]
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 0 a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 0 a[0] 0 a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 1 a[0] 0 a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 1 a[0] 0 a[1] 1 a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 2 a[0] 0 a[1] 1 a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 2 a[0] 0 a[1] 1 a[2] 2 a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 3 a[0] 0 a[1] 1 a[2] 2 a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 3 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 4 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 4 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 5 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 5 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 6 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

When the number of elements is odd, the middle element remains untouched.
Introduction
Code Action a[i] = i;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 6 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The swap loop still executes 7/2 = 3 times (integer division: ignore fraction).
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 7 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The swap loop still executes 7/2 = 3 times (integer division: ignore fraction).
Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action open scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action allocate t
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t ?
Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action t set to a[i]
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 0
Pointers
i 0 a[0] 0 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action a[i] = a[7-1-i];
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 0
Pointers
i 0 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 6

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action a[7-1-i] = t;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 0
Pointers
i 0 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action close scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 0 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 1 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action open scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 1 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action allocate t
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t ?
Pointers
i 1 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action t set to a[i]
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 1
Pointers
i 1 a[0] 6 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action a[i] = a[7-1-i];
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 1
Pointers
i 1 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 5 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action a[7-1-i] = t;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 1
Pointers
i 1 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action close scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 1 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 2 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action open scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 2 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action allocate t
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t ?
Pointers
i 2 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action t set to a[i]
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 2
Pointers
i 2 a[0] 6 a[1] 5 a[2] 2 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action a[i] = a[7-1-i];
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 2
Pointers
i 2 a[0] 6 a[1] 5 a[2] 4 a[3] 3 a[4] 4 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action a[7-1-i] = t;
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings
t 2
Pointers
i 2 a[0] 6 a[1] 5 a[2] 4 a[3] 3 a[4] 2 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action close scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 2 a[0] 6 a[1] 5 a[2] 4 a[3] 3 a[4] 2 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action for loop
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers
i 3 a[0] 6 a[1] 5 a[2] 4 a[3] 3 a[4] 2 a[5] 1 a[6] 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action close scope
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Reverse in place (odd size)

The last element is, however, 6 instead of 5.


Introduction
Code Action END PROGRAM
Input
/* array10.c */
Arrays
Intro
#include <stdio.h>
Access #define P 7
Input int main()
Array I/O
{
Output
Sum int i, a[P];
Odd Sum for(i=0; i<P; i++)
Copy a[i] = i;
Reverse
Flip
for(i=0; i<P/2 ; i++)
Odd Flip {
Float arrays int t;
Overrunning
Memory layout
t = a[i];
Bounds Errors a[i] = a[P-1-i];
Char arrays a[P-1-i] = t;
Overrunning chars
}
Scopes
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 41 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action start program
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action open scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action allocate i
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action allocate a[3], b[3], c[3]
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i ? a[0] ? a[1] ? a[2] ? b[0] ? b[1] ? b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] ? a[1] ? a[2] ? b[0] ? b[1] ? b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action input b[i], c[i]
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] ? b[2] ? c[0] 1.2 c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] ? b[2] ? c[0] 1.2 c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action input b[i], c[i]
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] ? c[0] 1.2 c[1] -2.0 c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] ? c[0] 1.2 c[1] -2.0 c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action input b[i], c[i]
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] 1.2 c[0] 1.2 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 3 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] 1.2 c[0] 1.2 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] 1.2 c[0] 1.2 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action open scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] ? a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] 1.2 c[0] 1.2 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action a[i] = b[i]*c[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] 3.0 a[1] ? a[2] ? b[0] 2.5 b[1] 3.5 b[2] 1.2 c[0] 1.2 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action b[i] = 1.0-c[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] 3.0 a[1] ? a[2] ? b[0] -0.2 b[1] 3.5 b[2] 1.2 c[0] 1.2 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action c[i] = 1.0+b[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] 3.0 a[1] ? a[2] ? b[0] -0.2 b[1] 3.5 b[2] 1.2 c[0] 0.8 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action close scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 0 a[0] 3.0 a[1] ? a[2] ? b[0] -0.2 b[1] 3.5 b[2] 1.2 c[0] 0.8 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] 3.0 a[1] ? a[2] ? b[0] -0.2 b[1] 3.5 b[2] 1.2 c[0] 0.8 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action open scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] 3.0 a[1] ? a[2] ? b[0] -0.2 b[1] 3.5 b[2] 1.2 c[0] 0.8 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action a[i] = b[i]*c[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] 3.0 a[1] -7.0 a[2] ? b[0] -0.2 b[1] 3.5 b[2] 1.2 c[0] 0.8 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action b[i] = 1.0-c[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] 3.0 a[1] -7.0 a[2] ? b[0] -0.2 b[1] 3.0 b[2] 1.2 c[0] 0.8 c[1] -2.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action c[i] = 1.0+b[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] 3.0 a[1] -7.0 a[2] ? b[0] -0.2 b[1] 3.0 b[2] 1.2 c[0] 0.8 c[1] 4.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action close scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 1 a[0] 3.0 a[1] -7.0 a[2] ? b[0] -0.2 b[1] 3.0 b[2] 1.2 c[0] 0.8 c[1] 4.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] 3.0 a[1] -7.0 a[2] ? b[0] -0.2 b[1] 3.0 b[2] 1.2 c[0] 0.8 c[1] 4.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action open scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] 3.0 a[1] -7.0 a[2] ? b[0] -0.2 b[1] 3.0 b[2] 1.2 c[0] 0.8 c[1] 4.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action a[i] = b[i]*c[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] 3.0 a[1] -7.0 a[2] 0.6 b[0] -0.2 b[1] 3.0 b[2] 1.2 c[0] 0.8 c[1] 4.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action b[i] = 1.0-c[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] 3.0 a[1] -7.0 a[2] 0.6 b[0] -0.2 b[1] 3.0 b[2] 0.5 c[0] 0.8 c[1] 4.0 c[2] 0.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action c[i] = 1.0+b[i];
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] 3.0 a[1] -7.0 a[2] 0.6 b[0] -0.2 b[1] 3.0 b[2] 0.5 c[0] 0.8 c[1] 4.0 c[2] 1.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action close scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 2 a[0] 3.0 a[1] -7.0 a[2] 0.6 b[0] -0.2 b[1] 3.0 b[2] 0.5 c[0] 0.8 c[1] 4.0 c[2] 1.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action for loop
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

i 3 a[0] 3.0 a[1] -7.0 a[2] 0.6 b[0] -0.2 b[1] 3.0 b[2] 0.5 c[0] 0.8 c[1] 4.0 c[2] 1.5

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action close scope
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Float Arrays
This slide describes how arrays of floats are used.
⇐ We will omit the menu on the left whenever we need extra space to show arrays.
Arrays of floats are manipulated in ways that are very similar to ints.
Of course, appropriate formatting is needed when reading data in and printing it out.
Code Action END PROGRAM
/* array11.c */
#include <stdio.h>
#define M 3 Input
int main() 2.5 1.2
{ 3.5 -2.0
int i; 1.2 0.5
float a[M], b[M], c[M];
for(i=0; i<M; i++)
scanf("%f %f", &b[i], &c[i]);
for(i=0; i<M; i++)
{
a[i] = b[i]*c[i];
b[i] = 1.0-c[i];
c[i] = 1.0+b[i];
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 42 / 77
Overrunning Arrays
/* array12.c */
If you declare an array to be of size 4, as in this example,
#include <stdio.h>
then you can only access the elements at indexes 0 to
int main()
3. Accesses outside this range will give you meaningless
{
information and may corrupt other variables.
int c, b, a[4];
The C programming language enforces no control over b = 5;
such out-of-bounds array accesses. It is up to you, the for(c=0; c<b; c++)
programmer, to ensure they do not occur. {
int d;
Out of bounds accesses to arrays often result in strange d = 9;
and inexplicable errors. The example program demon-
a[c] = d;
strates such a situation.
printf("%d %d %d\n", b,c,d);
A careful reading of this program will reveal that, al- }
though the the array a[4] only has elements a[0], . . . a[3], }
there are attempts to set a[4] and a[5] (which do not ex-
ist) to 9.
Such out-of-bounds accesses can have catastrophic con-
sequences, as illustrated in the following two pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 77
Overrunning Arrays
/* array12.c */
If you declare an array to be of size 4, as in this example,
#include <stdio.h>
then you can only access the elements at indexes 0 to
int main()
3. Accesses outside this range will give you meaningless
{
information and may corrupt other variables.
int c, b, a[4];
The C programming language enforces no control over b = 5;
such out-of-bounds array accesses. It is up to you, the for(c=0; c<b; c++)
programmer, to ensure they do not occur. {
int d;
Out of bounds accesses to arrays often result in strange d = 9;
and inexplicable errors. The example program demon-
a[c] = d;
strates such a situation.
printf("%d %d %d\n", b,c,d);
A careful reading of this program will reveal that, al- }
though the the array a[4] only has elements a[0], . . . a[3], }
there are attempts to set a[4] and a[5] (which do not ex-
ist) to 9.
Such out-of-bounds accesses can have catastrophic con-
sequences, as illustrated in the following two pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 77
Overrunning Arrays
/* array12.c */
If you declare an array to be of size 4, as in this example,
#include <stdio.h>
then you can only access the elements at indexes 0 to
int main()
3. Accesses outside this range will give you meaningless
{
information and may corrupt other variables.
int c, b, a[4];
The C programming language enforces no control over b = 5;
such out-of-bounds array accesses. It is up to you, the for(c=0; c<b; c++)
programmer, to ensure they do not occur. {
int d;
Out of bounds accesses to arrays often result in strange d = 9;
and inexplicable errors. The example program demon-
a[c] = d;
strates such a situation.
printf("%d %d %d\n", b,c,d);
A careful reading of this program will reveal that, al- }
though the the array a[4] only has elements a[0], . . . a[3], }
there are attempts to set a[4] and a[5] (which do not ex-
ist) to 9.
Such out-of-bounds accesses can have catastrophic con-
sequences, as illustrated in the following two pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 77
Overrunning Arrays
/* array12.c */
If you declare an array to be of size 4, as in this example,
#include <stdio.h>
then you can only access the elements at indexes 0 to
int main()
3. Accesses outside this range will give you meaningless
{
information and may corrupt other variables.
int c, b, a[4];
The C programming language enforces no control over b = 5;
such out-of-bounds array accesses. It is up to you, the for(c=0; c<b; c++)
programmer, to ensure they do not occur. {
int d;
Out of bounds accesses to arrays often result in strange d = 9;
and inexplicable errors. The example program demon-
a[c] = d;
strates such a situation.
printf("%d %d %d\n", b,c,d);
A careful reading of this program will reveal that, al- }
though the the array a[4] only has elements a[0], . . . a[3], }
there are attempts to set a[4] and a[5] (which do not ex-
ist) to 9.
Such out-of-bounds accesses can have catastrophic con-
sequences, as illustrated in the following two pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 77
Overrunning Arrays
/* array12.c */
If you declare an array to be of size 4, as in this example,
#include <stdio.h>
then you can only access the elements at indexes 0 to
int main()
3. Accesses outside this range will give you meaningless
{
information and may corrupt other variables.
int c, b, a[4];
The C programming language enforces no control over b = 5;
such out-of-bounds array accesses. It is up to you, the for(c=0; c<b; c++)
programmer, to ensure they do not occur. {
int d;
Out of bounds accesses to arrays often result in strange d = 9;
and inexplicable errors. The example program demon-
a[c] = d;
strates such a situation.
printf("%d %d %d\n", b,c,d);
A careful reading of this program will reveal that, al- }
though the the array a[4] only has elements a[0], . . . a[3], }
there are attempts to set a[4] and a[5] (which do not ex-
ist) to 9.
Such out-of-bounds accesses can have catastrophic con-
sequences, as illustrated in the following two pages.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 43 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
Memory Layout
/* array12.c */
Up to this point in our eBook, we have usually laid out
#include <stdio.h>
our variables in alphabetical order for your convenience.
int main()
To explore array out of bound errors, we need to display {
our variables in the way they are actually laid out in a real int c, b, a[4];
computer system (which is not a convenient alphabetic b = 5;
order). for(c=0; c<b; c++)
{
The example program declares: int d;
int d, c, b, a[4]; d = 9;
These would typically be laid out in memory in a right-
a[c] = d;
to-left order, but with the array elements in left-to right
printf("%d %d %d\n", b,c,d);
order relative to each other.
}
Thus the variables of this program would be placed in }
memory in the following order:
a[0] a[1] a[2] a[3] b c d

The reasons for this layout are beyond the scope of this
eBook.
In the animations that follow, we will use this ‘true’ layout
whenever we are explaining array out of bounds errors and
related issues.
We will use the convenient (usually alphabetic) layout in
other situations.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 44 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action start program


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate c, b, a[4]


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] ? a[1] ? a[2] ? a[3] ? b ? c ?
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action b set to 5


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] ? a[1] ? a[2] ? a[3] ? b 5 c ?
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] ? a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] ? a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d ?
Scopes
Exercises a[0] ? a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action d set to 9


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] ? a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

Set a[0] to 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action a[c] = d;


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action output b,c,d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 0
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

Set a[1] to 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d ?
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action d set to 9


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] ? a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action a[c] = d;


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4];
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action output b,c,d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 1
Strings

Pointers
Memory

Set a[2] to 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 2
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 2
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d ?
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 2
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action d set to 9


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] ? a[3] ? b 5 c 2
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action a[c] = d;


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5;
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 2
Strings

Pointers
Memory

Set a[3] to 9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action output b,c,d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 2
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 2
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 3
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 3
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d ?
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 3
Strings

Pointers
Memory

Set a[4] to 9. No such variable! 9 is written into the variable that happens to be in
memory at the location a[4] would have been, if it existed. This is b.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action d set to 9


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] ? b 5 c 3
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action a[c] = d;


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++)
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 3
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action output b,c,d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 3
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 3
Strings

Pointers
Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 4
Strings

Pointers
Memory

Set a[5] to 9. No such variable! 9 is written into the variable that happens to be in
memory at the location a[5] would have been, if it existed. This is c.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 4
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d ?
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 4
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action d set to 9


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 5 c 4
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action a[c] = d;


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum {
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 4
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action output b,c,d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 4
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 4
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 5
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action open scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 5
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action allocate d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d ?
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 5
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action d set to 9


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 5
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action a[c] = d;


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d;
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 9
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action output b,c,d


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d; 9 9 9
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars d 9
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 9
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d; 9 9 9
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 9
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action for loop


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d; 9 9 9
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises a[0] 9 a[1] 9 a[2] 9 a[3] 9 b 9 c 9
Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action close scope


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d; 9 9 9
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Array out-of-bounds errors

Code Action END PROGRAM


Introduction
/* array12.c */
Input
#include <stdio.h>
Arrays int main() Output
Intro
Access
{ 5 0 9
Input int c, b, a[4]; 5 1 9
Array I/O b = 5; 5 2 9
Output
for(c=0; c<b; c++) 5 3 9
Sum
Odd Sum { 9 4 9
Copy int d; 9 9 9
Reverse d = 9;
Flip
Odd Flip
a[c] = d;
Float arrays printf("%d %d %d\n", b,c,d);
Overrunning }
Memory layout
Bounds Errors
}
Char arrays
Overrunning chars
Scopes
Exercises

Strings

Pointers
Memory

The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
Overwriting of variables by out of bound array accesses can have strange and
unexpected results.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 45 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action start program


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action open scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action allocate i


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action allocate b[3], c[3]


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i ? b[0] ? b[1] ? b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 0 b[0] ? b[1] ? b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action b[i] = ’a’;


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 0 b[0] ’a’ b[1] ? b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 1 b[0] ’a’ b[1] ? b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action b[i] = ’a’;


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 1 b[0] ’a’ b[1] ’a’ b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 2 b[0] ’a’ b[1] ’a’ b[2] ? c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action b[i] = ’a’;


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 3 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action open scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action allocate t


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ?

i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action t set to b[i] + i


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ’a’

i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ? c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action c[i] = t;


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ’a’

i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action close scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action open scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action allocate t


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ?

i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action t set to b[i] + i


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ’b’

i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action c[i] = t;


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ’b’

i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action close scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action open scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action allocate t


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ?

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action t set to b[i] + i


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ’c’

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action c[i] = t;


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers
t ’c’

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ’c’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action close scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ’c’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action for loop


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

i 3 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ’c’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action close scope


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Char Arrays
Arrays of char are manipulated in the same way as int or float arrays. However,
interpretation of their contents is different, as they store ASCII codes. We show
Introduction
that incrementing a char variable changes the contents in a predictable fashion.
Input

Arrays Code Action END PROGRAM


Intro /* array13.c */
Access
#include <stdio.h>
Input
Array I/O #define M 3
Output int main()
Sum {
Odd Sum
Copy
int i;
Reverse char b[M], c[M];
Flip for(i=0; i<M; i++)
Odd Flip
b[i] = ’a’;
Float arrays
Overrunning for(i=0; i<M; i++)
Memory layout {
Bounds Errors char t;
Char arrays
Overrunning chars
t = b[i] + i;
Scopes c[i] = t;
Exercises }
Strings }
Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 46 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action start program
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action open scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action allocate a
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

a ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action allocate z, b[3]
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ? b[1] ? b[2] ? z ? a ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action z set to ’p’
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ? b[1] ? b[2] ? z ’p’ a ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action for loop
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ? b[1] ? b[2] ? z ’p’ a 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action open scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ? b[1] ? b[2] ? z ’p’ a 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action b[a] = ’x’;
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main()
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ? b[2] ? z ’p’ a 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action output z,a
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ? b[2] ? z ’p’ a 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action close scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ? b[2] ? z ’p’ a 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action for loop
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ? b[2] ? z ’p’ a 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action open scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ? b[2] ? z ’p’ a 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action b[a] = ’x’;
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy {
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ? z ’p’ a 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action output z,a
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ? z ’p’ a 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action close scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ? z ’p’ a 1

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action for loop
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ? z ’p’ a 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action open scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ? z ’p’ a 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action b[a] = ’x’;
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a;
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’p’ a 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action output z,a
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’p’ a 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action close scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’p’ a 2

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action for loop
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’p’ a 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action open scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’p’ a 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action b[a] = ’x’;
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M];
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action output z,a
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action close scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 3

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action for loop
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action open scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 4

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action b[a] = ’x’;
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’;
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 120

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action output z,a
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’; x 120
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 120

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action close scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’; x 120
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 120

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action for loop
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’; x 120
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

b[0] ’x’ b[1] ’x’ b[2] ’x’ z ’x’ a 121

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action close scope
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’; x 120
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Overrunning char arrays

Out of bound array errors can lead to unexpected behavior in char arrays as well.
Introduction
Note how the int variable a becomes 120: this is the ASCII value of ’x’.
Input
The loop executes 5 times instead of 10. This is because the for variable a is
overwritten with 120: this immediately terminates the loop.
Arrays
Intro Code Action END PROGRAM
Access
Input
/* array14.c */
Array I/O #include <stdio.h>
Output #define M 3 Output
Sum
Odd Sum
int main() p 0
Copy { p 1
Reverse int a; p 2
Flip
char z, b[M]; x 3
Odd Flip
Float arrays z = ’p’; x 120
Overrunning for(a=0; a<10; a++)
Memory layout {
Bounds Errors
Char arrays
b[a] = ’x’;
Overrunning chars printf("%c %d\n",z,a);
Scopes }
Exercises
}
Strings

Pointers

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 47 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action start program
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action open scope
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action allocate i, a[3], b[4]
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i ? a[0] ? a[1] ? a[2] ? b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 0 a[0] ? a[1] ? a[2] ? b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action a[i] = i;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 0 a[0] 0 a[1] ? a[2] ? b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 1 a[0] 0 a[1] ? a[2] ? b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action a[i] = i;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 1 a[0] 0 a[1] 1 a[2] ? b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 2 a[0] 0 a[1] 1 a[2] ? b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action a[i] = i;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 2 a[0] 0 a[1] 1 a[2] 2 b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 3 a[0] 0 a[1] 1 a[2] 2 b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 0 a[0] 0 a[1] 1 a[2] 2 b[0] ? b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = i + 40;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 0 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 1 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] ? b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = i + 40;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 1 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 2 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] ? b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = i + 40;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 2 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 3 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = i + 40;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 3 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 4 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action open scope
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 4 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action allocate b[4]
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 4 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 0 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action a[i] = 2*i;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 0 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 1 a[0] 0 a[1] 1 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action a[i] = 2*i;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 1 a[0] 0 a[1] 2 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 2 a[0] 0 a[1] 2 a[2] 2 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action a[i] = 2*i;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 2 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 3 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ? b[1] ? b[2] ? b[3] ?

i 0 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = 2*i+50;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ? b[2] ? b[3] ?

i 0 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ? b[2] ? b[3] ?

i 1 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = 2*i+50;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ’4’ b[2] ? b[3] ?

i 1 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ’4’ b[2] ? b[3] ?

i 2 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = 2*i+50;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ’4’ b[2] ’6’ b[3] ?

i 2 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ’4’ b[2] ’6’ b[3] ?

i 3 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action b[i] = 2*i+50;
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ’4’ b[2] ’6’ b[3] ’8’

i 3 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action for loop
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

b[0] ’2’ b[1] ’4’ b[2] ’6’ b[3] ’8’

i 4 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action close scope
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

i 4 a[0] 0 a[1] 2 a[2] 4 b[0] 40 b[1] 41 b[2] 42 b[3] 43

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action close scope
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 48 / 77
Arrays & Scopes: Arrays are subject to the same scope rules as ordinary variables. We show an int array
being shadowed by a char array with the same name, b.
Code Action END PROGRAM
/* array15.c */
#include <stdio.h>
#define M 3
#define N 4
int main()
{
int i, a[M], b[N];
for(i=0; i<M; i++)
a[i] = i;
for(i=0; i<N; i++)
b[i] = i + 40;
{
char b[N];
for(i=0; i<M; i++)
a[i] = 2*i;
for(i=0; i<N; i++)
b[i] = 2*i+50;
}
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


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

Exercise A1: Write a program that declares an int array of size 20. It then reads in
Introduction
a sequence of integers into this array. The input is guaranteed to contain at least
Input
one integer, but may exceed 20 integers. If the number of inputs exceeds 20, the
program only considers the first 20 and ignores the rest. After reading in the data,
Arrays
Intro
the program computes the maximum and minimum values and prints these out
Access along with the length of the data input. For example, if input:
Input
Array I/O
8
Output 3
Sum -9
Odd Sum
Copy
11
Reverse 2
Flip
Odd Flip
The program should print:
Float arrays Len= 5 max= 11 min=-9
Overrunning
Memory layout
Exercise A2: Modify the above program so that it also prints out the position of the
Bounds Errors
Char arrays
maximum and minimum. For example, for the input shown above, it should output:
Overrunning chars
Len= 5 max= 11 (at 3), min= -9 (at 2)
Scopes
Exercises
Exercise A3: Modify the program in Exercise A1 so that, if input is longer than 20
Strings
elements, it considers only the last 20. That is, if 25 numbers are entered, the first
5 are ignored and max and min computed for the last 20. The int array used must
Pointers
still be of size 20.
Exercise A4: Modify the program in Exercise A2 so that it fulfills the additional
constraints stated in Exercise A3. The positions printed out must be with respect
to the input data, and not the size of the int array. That is, if the 27th element
input is maximum, the program must output 27.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 49 / 77
C Programming:
Memory & Pointers
An Algopath eBook

Introduction

Input

Arrays

Strings
Intro
String I/O
Inside Strings
Copying
strcat(), strlen()
Constants
Comparisons Strings
Modification
Exercises S1–S3
Exercises S4–S5
Exercise S6

Pointers

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 50 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Introduction to Strings

Introduction

Input
Strings are arrays of char that are terminated with a special character called
null, denoted ’\0’.
Arrays

Strings String ”The crow” could be stored in a char array x[9] as follows:
Intro x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8]
String I/O
Inside Strings
’T’ ’h’ ’e’ ’ ’ ’c’ ’r’ ’o’ ’w’ ’\0’
Copying
strcat(), strlen()
Notice that each character in ”The crow” occupies one location of x[9]. In
Constants addition a location is required for the terminating null ’\0’.
Comparisons
Modification The start of a string stored in char array c is at c[0].
Exercises S1–S3
Exercises S4–S5 The end of the string is just before the first occurrence of ’\0’, after c[0].
Exercise S6
Consider the following array c[8]:
Pointers
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7]
’r’ ’a’ ’v’ ’e’ ’n’ ’\0’ ’?’ ’?’

The above array contains the string ”raven”. The characters that occur after
the ’\0’ are ignored.
It is your responsibility to ensure that the character array you are using is large
enough for the string. For example, ”raven” cannot be stored in an array of size
less than 6.
In the following we explain the behavior of strings and describe some of the
powerful facilities available for working with strings.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 51 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action start program


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

Memory

Strings are read in with scanf using the ”%s” format specification. An ampersand
(&) is not used, as shown above.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action open scope


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

Memory

Strings are read in with scanf using the ”%s” format specification. An ampersand
(&) is not used, as shown above.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action allocate a[6]


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ?

Memory

Strings are read in with scanf using the ”%s” format specification. An ampersand
(&) is not used, as shown above.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action input a


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ’h’ a[1] ’i’ a[2] ’\0’ a[3] ? a[4] ? a[5] ?

Memory

When reading in a string, only as much of the array is used as is needed. “hi” only
needs 3 locations (including the NULL), the remaining locations are ignored.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action output a


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ’h’ a[1] ’i’ a[2] ’\0’ a[3] ? a[4] ? a[5] ?

Memory

When printing out string a, printf starts at a[0] and prints each character up to but
excluding the first NULL. The remaining characters are ignored.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action input a


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ’h’ a[1] ’e’ a[2] ’l’ a[3] ’l’ a[4] ’o’ a[5] ’\0’

Memory

A subsequent scanf into the same string will overwrite the previous string.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action output a


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ’h’ a[1] ’e’ a[2] ’l’ a[3] ’l’ a[4] ’o’ a[5] ’\0’

Memory

A subsequent scanf into the same string will overwrite the previous string.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action input a


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

When overwriting with a shorter string than the preceding string, part of the
preceding string will remain.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action output a


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

However any overwritten parts will be ignored, since the string, by definition, starts
at a[0] and extends up to the first NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action close scope


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

Memory

However any overwritten parts will be ignored, since the string, by definition, starts
at a[0] and extends up to the first NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
String input/output

Code Action END PROGRAM


Introduction
/* string01.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings int main() hi
Intro
{ hello
String I/O
Inside Strings char a[6]; bye
Copying scanf("%s", a);
strcat(), strlen() printf("%s\n", a); Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a);
}
Pointers

Memory

However any overwritten parts will be ignored, since the string, by definition, starts
at a[0] and extends up to the first NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 52 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action start program


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action open scope


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action allocate i


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ?

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action allocate a[6]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ?

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action input a


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a);
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ’h’ a[1] ’i’ a[2] ’\0’ a[3] ? a[4] ? a[5] ?

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output a


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ’h’ a[1] ’i’ a[2] ’\0’ a[3] ? a[4] ? a[5] ?

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action input a


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a);
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ’h’ a[1] ’e’ a[2] ’l’ a[3] ’l’ a[4] ’o’ a[5] ’\0’

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output a


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ’h’ a[1] ’e’ a[2] ’l’ a[3] ’l’ a[4] ’o’ a[5] ’\0’

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action input a


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a);
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output a


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i ? a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

Explore what is stored inside a string using printf.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a);
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 0 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The printfs show the integer value at each location.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output (int)a[i]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 0 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 98 = ’b’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a);
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 1 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 98 = ’b’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output (int)a[i]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 1 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 121 = ’y’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a);
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 2 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 121 = ’y’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output (int)a[i]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 2 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 101 = ’e’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++)
printf("%d\n", (int)a[i]);
}

i 3 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 101 = ’e’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output (int)a[i]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]);
}

i 3 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The 0 represents the end-of-string NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]);
}

i 4 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The 0 represents the end-of-string NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output (int)a[i]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]); 111
}

i 4 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 111 = ’o’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]); 111
}

i 5 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The nonzero numbers are ASCII codes: 111 = ’o’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action output (int)a[i]


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]); 111
} 0

i 5 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The 0 represents the end-of-string NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action for loop


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]); 111
} 0

i 6 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’

Memory

The 0 represents the end-of-string NULL.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action close scope


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]); 111
} 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
C Programming:
Memory & Pointers
An Algopath eBook
What is stored in a String?

Code Action END PROGRAM


Introduction
/* string02.c */
Input
#include <stdio.h>
Arrays #include <string.h> Input
Strings #define MAX 6 hi
Intro
int main() hello
String I/O
Inside Strings { bye
Copying int i;
strcat(), strlen() char a[MAX]; Output
Constants
Comparisons
scanf("%s", a); hi
Modification printf("%s\n", a); hello
Exercises S1–S3 scanf("%s", a); bye
Exercises S4–S5
Exercise S6
printf("%s\n", a); 98
scanf("%s", a); 121
Pointers
printf("%s\n", a); 101
for(i=0; i<MAX; i++) 0
printf("%d\n", (int)a[i]); 111
} 0

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 53 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action start program


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN];
scanf("%s", a);
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action open scope


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN];
scanf("%s", a);
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action allocate a[5], b[5]


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN];
scanf("%s", a);
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action input a


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN];
scanf("%s", a);
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action strcpy(b, a);


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN];
scanf("%s", a);
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ? b[0] ’b’ b[1] ’y’ b[2] ’e’ b[3] ’\0’ b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action output a


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN]; bye
scanf("%s", a);
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ? b[0] ’b’ b[1] ’y’ b[2] ’e’ b[3] ’\0’ b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action output b


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN]; bye
scanf("%s", a); bye
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ? b[0] ’b’ b[1] ’y’ b[2] ’e’ b[3] ’\0’ b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action close scope


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN]; bye
scanf("%s", a); bye
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
Copying strings with strcpy()
The strcpy() function lets you copy one string to another.
The destination string must have room to hold the copied string.
strcpy() is useful since you cannot use assignment (e.g. b = a) to copy strings.

Code Action END PROGRAM


/* string03.c */
#include <stdio.h>
#include <string.h> Input
#define LEN 5 bye
int main()
{ Output
char a[LEN], b[LEN]; bye
scanf("%s", a); bye
strcpy(b, a);
printf("%s\n", a);
printf("%s\n", b);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 54 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action start program


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b);
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action open scope


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b);
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action allocate a[7], b[4]


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b);
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

b[0] ? b[1] ? b[2] ? b[3] ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action input a


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b);
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

b[0] ? b[1] ? b[2] ? b[3] ? a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action input b


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b);
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

b[0] ’c’ b[1] ’a’ b[2] ’t’ b[3] ’\0’ a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ a[4] ? a[5] ? a[6] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action strcat(a, b);


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b);
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

b[0] ’c’ b[1] ’a’ b[2] ’t’ b[3] ’\0’ a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’c’ a[4] ’a’ a[5] ’t’ a[6] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action output


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b); fatcat 6
strcat(a, b);
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

b[0] ’c’ b[1] ’a’ b[2] ’t’ b[3] ’\0’ a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’c’ a[4] ’a’ a[5] ’t’ a[6] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action output


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b); fatcat 6
strcat(a, b); cat 3
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

b[0] ’c’ b[1] ’a’ b[2] ’t’ b[3] ’\0’ a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’c’ a[4] ’a’ a[5] ’t’ a[6] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action close scope


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b); fatcat 6
strcat(a, b); cat 3
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
strcat() and strlen()
The strcat(a,b) function catenates (i.e. appends) string b to the end of string a.
The length of a string is determined by the position of the \0.
strlen() conveniently provides this information.

Code Action END PROGRAM


/* string04.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[7], b[4];
scanf("%s", a); Output
scanf("%s", b); fatcat 6
strcat(a, b); cat 3
printf("%s %d\n", a, strlen(a));
printf("%s %d\n", b, strlen(b));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 55 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action start program


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action open scope


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action allocate a[9], b[2]


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ? a[7] ? a[8] ? b[0] ? b[1] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action strcpy(a,”fat”);


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ a[4] ? a[5] ? a[6] ? a[7] ? a[8] ? b[0] ? b[1] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action strcpy(b,” ”);


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ a[4] ? a[5] ? a[6] ? a[7] ? a[8] ? b[0] ’ ’ b[1] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action strcat(a,b);


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’ ’ a[4] ’\0’ a[5] ? a[6] ? a[7] ? a[8] ? b[0] ’ ’ b[1] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action strcat(a,”cat”);


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," ");
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’ ’ a[4] ’c’ a[5] ’a’ a[6] ’t’ a[7] ’\0’ a[8] ? b[0] ’ ’ b[1] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action output


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," "); fat cat 7
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’ ’ a[4] ’c’ a[5] ’a’ a[6] ’t’ a[7] ’\0’ a[8] ? b[0] ’ ’ b[1] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action strcat(a, ”s”);


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," "); fat cat 7
strcat(a,b);
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’ ’ a[4] ’c’ a[5] ’a’ a[6] ’t’ a[7] ’s’ a[8] ’\0’ b[0] ’ ’ b[1] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action output


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," "); fat cat 7
strcat(a,b); fat cats 8
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

a[0] ’f’ a[1] ’a’ a[2] ’t’ a[3] ’ ’ a[4] ’c’ a[5] ’a’ a[6] ’t’ a[7] ’s’ a[8] ’\0’ b[0] ’ ’ b[1] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action close scope


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," "); fat cat 7
strcat(a,b); fat cats 8
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
String Constants
Any sequence of characters within a pair of double quotes is a string constant.
The following program gives some examples of the use of string constants.

Code Action END PROGRAM


/* string05.c */
#include <stdio.h>
#include <string.h> Input
int main() fat
{ cat
char a[9], b[2];
strcpy(a,"fat"); Output
strcpy(b," "); fat cat 7
strcat(a,b); fat cats 8
strcat(a,"cat");
printf("%s %d\n", a, strlen(a));
strcat(a, "s");
printf("%s %d\n", a, strlen(a));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 56 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action start program


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action open scope


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action allocate a[5], b[5]


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action strcpy(a,”pot”);


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ’p’ a[1] ’o’ a[2] ’t’ a[3] ’\0’ a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? b[4] ?

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action strcpy(b,”pots”);


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ’p’ a[1] ’o’ a[2] ’t’ a[3] ’\0’ a[4] ? b[0] ’p’ b[1] ’o’ b[2] ’t’ b[3] ’s’ b[4] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action output strcmp(a,b)


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main() -115
{
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ’p’ a[1] ’o’ a[2] ’t’ a[3] ’\0’ a[4] ? b[0] ’p’ b[1] ’o’ b[2] ’t’ b[3] ’s’ b[4] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action output strcmp(b,a)


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main() -115
{ 115
char a[5], b[5];
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ’p’ a[1] ’o’ a[2] ’t’ a[3] ’\0’ a[4] ? b[0] ’p’ b[1] ’o’ b[2] ’t’ b[3] ’s’ b[4] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action output


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main() -115
{ 115
char a[5], b[5]; 0
strcpy(a,"pot");
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ’p’ a[1] ’o’ a[2] ’t’ a[3] ’\0’ a[4] ? b[0] ’p’ b[1] ’o’ b[2] ’t’ b[3] ’s’ b[4] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action output


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main() -115
{ 115
char a[5], b[5]; 0
strcpy(a,"pot"); 16
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

a[0] ’p’ a[1] ’o’ a[2] ’t’ a[3] ’\0’ a[4] ? b[0] ’p’ b[1] ’o’ b[2] ’t’ b[3] ’s’ b[4] ’\0’

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action close scope


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main() -115
{ 115
char a[5], b[5]; 0
strcpy(a,"pot"); 16
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Comparing strings with strcmp()
We can compare two strings to see their dictionary order, i.e. whether one comes before the other in the
dictionary.
The function strcpm(a,b) returns a negative, zero or positive integer depending on whether a occurs
before b, is equal to b or occurs after b.

Code Action END PROGRAM


/* string06.c */
#include <stdio.h>
#include <string.h> Output
int main() -115
{ 115
char a[5], b[5]; 0
strcpy(a,"pot"); 16
strcpy(b,"pots");
printf("%d\n",strcmp(a,b));
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,"pot"));
printf("%d\n",strcmp(a,"pod"));
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 57 / 77
Modifying Strings:

Code Action start program


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

Memory
Strings can be modified by accessing the corresponding char array. If you are not careful, you may get
strange results.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action open scope


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

Memory
Strings can be modified by accessing the corresponding char array. If you are not careful, you may get
strange results.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action allocate c[4], b[3], a[4]


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ? a[1] ? a[2] ? a[3] ? b[0] ? b[1] ? b[2] ? c[0] ? c[1] ? c[2] ? c[3] ?

Memory
By declaring the char in the order c[4], b[3], a[4], we cause them to be laid out in memory as shown
above.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action strcpy(a,”cat”);


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ b[0] ? b[1] ? b[2] ? c[0] ? c[1] ? c[2] ? c[3] ?

Memory
The strings are set up.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action strcpy(b,”my”);


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ b[0] ’m’ b[1] ’y’ b[2] ’\0’ c[0] ? c[1] ? c[2] ? c[3] ?

Memory
The strings are set up.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action strcpy(c,”and”);


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main()
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ b[0] ’m’ b[1] ’y’ b[2] ’\0’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory
The strings are set up.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action output a, b, c


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’\0’ b[0] ’m’ b[1] ’y’ b[2] ’\0’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory
String a starts at a[0] and continues until a \0 is encountered. To print a, b or c we start at a[0], b[0] or
c[0], respectively and continue until a \0 is encountered.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action a[3]=97;


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’y’ b[2] ’\0’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory
Element 3 of string a is set to 97=ASCII ’a’–this overwrites the NULL marking the end of this string.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action output a, b, c


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’y’ b[2] ’\0’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory
To print a, we start at a[0] and continue until a \0 is encountered, thus a = “catamy”. To print b, we
start at b[0] and continue until a \0 is encountered, thus b = “my”. Similarly c = “and”.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action b[1]=97;


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4];
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’a’ b[2] ’\0’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory
Element 1 of string b which is ’y’ is set to 97=ASCII ’a’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action output a, b, c


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’a’ b[2] ’\0’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory

To print a, we start at a[0] and continue until a \0, thus a = “catama”. To print b, we start at b[0] and
continue until a \0, thus b = “ma”. Similarly c = “and”.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action b[2]=114;


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat");
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’a’ b[2] ’r’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory

Element 2 of string b is set to 114=ASCII ’r’.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action output a, b, c


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat"); catamarand marand and
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’a’ b[2] ’r’ c[0] ’a’ c[1] ’n’ c[2] ’d’ c[3] ’\0’

Memory

To print a, we start at a[0] and continue until a \0 is encountered, thus a = “catamarand”. To print b,
we start at b[0] and continue until a \0, thus b = “marand”. Similarly c = “and”.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action c[2]=0;


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat"); catamarand marand and
strcpy(b,"my");
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’a’ b[2] ’r’ c[0] ’a’ c[1] ’n’ c[2] ’\0’ c[3] ’\0’

Memory

Element 2 of string c is set to 0 (NULL).

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action output a, b, c


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat"); catamarand marand and
strcpy(b,"my"); catamaran maran an
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

a[0] ’c’ a[1] ’a’ a[2] ’t’ a[3] ’a’ b[0] ’m’ b[1] ’a’ b[2] ’r’ c[0] ’a’ c[1] ’n’ c[2] ’\0’ c[3] ’\0’

Memory

To print a, we start at a[0] and continue until a \0 is encountered, thus a = “catamaran”. To print b,
we start at b[0] and continue until a \0 is encountered, thus b = “maran”. Similarly c = “an”.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action close scope


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat"); catamarand marand and
strcpy(b,"my"); catamaran maran an
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

Memory

To print a, we start at a[0] and continue until a \0 is encountered, thus a = “catamaran”. To print b,
we start at b[0] and continue until a \0 is encountered, thus b = “maran”. Similarly c = “an”.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 58 / 77
Modifying Strings:

Code Action END PROGRAM


/* string07.c */
#include <stdio.h>
#include <string.h> Output
int main() cat my and
{ catamy my and
char c[4], b[3], a[4]; catama ma and
strcpy(a,"cat"); catamarand marand and
strcpy(b,"my"); catamaran maran an
strcpy(c,"and");
printf("%s %s %s\n", a, b, c);
a[3]=97;
printf("%s %s %s\n", a, b, c);
b[1]=97;
printf("%s %s %s\n", a, b, c);
b[2]=114;
printf("%s %s %s\n", a, b, c);
c[2]=0;
printf("%s %s %s\n", a, b, c);
}

Memory

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 58 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercises S1–S3

Introduction
Exercise S1: (a) What does the following program achieve and how? (b) Can we
Input replace the test “i < strlen(s)” with “i < 12”? Explain.
Arrays
/* string02s.c */
Strings
#include <stdio.h>
Intro
String I/O #include <string.h>
Inside Strings int main()
Copying {
strcat(), strlen()
Constants
int i;
Comparisons char s[12];
Modification strcpy(s,"abcdefghi");
Exercises S1–S3
printf("%s\n", s);
Exercises S4–S5
Exercise S6 for(i=0; i < strlen(s); i++)
Pointers
s[i] = s[i] - ’a’ + ’A’;
printf("%s\n", s);
}
Exercise S2: Write a program that will input a string and then print it out in
reverse. For example, if input “abracadabra”, it should print out “arbadacarba”.
Exercise S3: A palindrome is a string that reads the same forwards and backwards,
e.g. “nolemonsnomelon” or “amanaplanacanalpanama”. Modify the above
program so that it prints “yes” if the input is a palindrome and “no” otherwise.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 59 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercises S4–S5

Introduction Exercise S4: Write a program that will input several lines of text from the keyboard
Input (or, via redirection, from a file). Each input line is guaranteed to be no longer than
Arrays 120 characters. When input has terminated, the program should print out a
Strings distribution of alphabetic characters. Non alphabetic characters are ignored and
Intro lower-case letters are lumped with their upper-case versions. For example, if input:
String I/O
Inside Strings The Quick Brown Fox
Copying Jumped Over The Lazy Dog’s Tail!
strcat(), strlen()
Constants The output should be:
Comparisons
Modification
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Exercises S1–S3 2 1 1 2 4 1 1 2 2 1 1 2 1 1 4 1 1 2 1 3 2 1 1 1 1 1
Exercises S4–S5
Exercise S6
Note: This program is about 25 lines in length.
Pointers Exercise S5: Write a program that inputs three strings (of length at most 10 chars)
and then prints them out in dictionary order. For example, if input the strings:
anteater
anodyne
antidote
It should print out:
anodyne
anteater
antidote
Note: This program uses the strcmp() function and nested if statements. It is
about 30 lines in length.
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 60 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise S6

Introduction
Exercise S6: Complete the following program (fill in the ?s), given that its output
Input is: surgeon.
Arrays
/* string07s.c */
Strings
#include <stdio.h>
Intro
String I/O #include <string.h>
Inside Strings int main()
Copying {
strcat(), strlen()
Constants
int i, p;
Comparisons char a[10], b[10], c[10];
Modification strcpy(a,"sturdy");
Exercises S1–S3
strcpy(b,"only");
Exercises S4–S5
Exercise S6 strcpy(c,"urgent");
Pointers
?[?] = 0;
str???(?,?);
?[?] = 0;
str???(?,?);
?[?] = 0;
printf("%s\n",a);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 61 / 77
C Programming:
Memory & Pointers
An Algopath eBook

Introduction

Input

Arrays

Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Deleting
Swapping
Pointers
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays

Strings A pointer does not hold any information,


Pointers it ‘points’ to another variable (sometimes
Intro called a pointee).
Example
Dereferencing To declare a pointer we could say
Pointers & Pointees
int *p;
Deleting
Swapping which would declare p to be a pointer to
int pointers an int.
char pointers
int & char pointers To initialize a pointer, we need to set it to
Pointers to Pointers point to something. This could be done,
Function Parameters
Animation
for example, by:
Exercise P1: p = &x;
Exercise P2: which means “p is loaded with the address
Exercises P3-P6
of x,” or “p points to x.”
To access the data that p points to, we use
the notation *p, which means “the con-
tents of the location pointed to by p.”
In the above context, * is called the deref-
erencing operator.

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays

Strings A pointer does not hold any information,


Pointers it ‘points’ to another variable (sometimes
Intro called a pointee).
Example
Dereferencing To declare a pointer we could say
Pointers & Pointees
int *p;
Deleting
Swapping which would declare p to be a pointer to
int pointers an int.
char pointers
int & char pointers To initialize a pointer, we need to set it to
Pointers to Pointers point to something. This could be done,
Function Parameters
Animation
for example, by:
Exercise P1: p = &x;
Exercise P2: which means “p is loaded with the address
Exercises P3-P6
of x,” or “p points to x.”
To access the data that p points to, we use
the notation *p, which means “the con-
tents of the location pointed to by p.”
In the above context, * is called the deref-
erencing operator.

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays

Strings A pointer does not hold any information,


Pointers it ‘points’ to another variable (sometimes
Intro called a pointee).
Example
Dereferencing To declare a pointer we could say
Pointers & Pointees
int *p;
Deleting
Swapping which would declare p to be a pointer to
int pointers an int.
char pointers
int & char pointers To initialize a pointer, we need to set it to
Pointers to Pointers point to something. This could be done,
Function Parameters
Animation
for example, by:
Exercise P1: p = &x;
Exercise P2: which means “p is loaded with the address
Exercises P3-P6
of x,” or “p points to x.”
To access the data that p points to, we use
the notation *p, which means “the con-
tents of the location pointed to by p.”
In the above context, * is called the deref-
erencing operator.

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays

Strings A pointer does not hold any information,


Pointers it ‘points’ to another variable (sometimes
Intro called a pointee).
Example
Dereferencing To declare a pointer we could say
Pointers & Pointees
int *p;
Deleting
Swapping which would declare p to be a pointer to
int pointers an int.
char pointers
int & char pointers To initialize a pointer, we need to set it to
Pointers to Pointers point to something. This could be done,
Function Parameters
Animation
for example, by:
Exercise P1: p = &x;
Exercise P2: which means “p is loaded with the address
Exercises P3-P6
of x,” or “p points to x.”
To access the data that p points to, we use
the notation *p, which means “the con-
tents of the location pointed to by p.”
In the above context, * is called the deref-
erencing operator.

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays

Strings A pointer does not hold any information,


Pointers it ‘points’ to another variable (sometimes
Intro called a pointee).
Example
Dereferencing To declare a pointer we could say
Pointers & Pointees
int *p;
Deleting
Swapping which would declare p to be a pointer to
int pointers an int.
char pointers
int & char pointers To initialize a pointer, we need to set it to
Pointers to Pointers point to something. This could be done,
Function Parameters
Animation
for example, by:
Exercise P1: p = &x;
Exercise P2: which means “p is loaded with the address
Exercises P3-P6
of x,” or “p points to x.”
To access the data that p points to, we use
the notation *p, which means “the con-
tents of the location pointed to by p.”
In the above context, * is called the deref-
erencing operator.

c 2008 Algopath LLC. All Rights Reserved.


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

Introduction

Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays

Strings A pointer does not hold any information,


Pointers it ‘points’ to another variable (sometimes
Intro called a pointee).
Example
Dereferencing To declare a pointer we could say
Pointers & Pointees
int *p;
Deleting
Swapping which would declare p to be a pointer to
int pointers an int.
char pointers
int & char pointers To initialize a pointer, we need to set it to
Pointers to Pointers point to something. This could be done,
Function Parameters
Animation
for example, by:
Exercise P1: p = &x;
Exercise P2: which means “p is loaded with the address
Exercises P3-P6
of x,” or “p points to x.”
To access the data that p points to, we use
the notation *p, which means “the con-
tents of the location pointed to by p.”
In the above context, * is called the deref-
erencing operator.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 63 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When a pointer is declared it does not


Introduction
point to anything specific, just as an ordi-
Input
nary variable, when declared, contains an
unknown value.
Arrays

Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action start program
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p);
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When a pointer is declared it does not


Introduction
point to anything specific, just as an ordi-
Input
nary variable, when declared, contains an
unknown value.
Arrays

Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action open scope
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p);
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When a pointer is declared it does not


Introduction
point to anything specific, just as an ordi-
Input
nary variable, when declared, contains an
unknown value.
Arrays

Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action allocate x, y
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x ? y ?
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p);
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When a pointer is declared it does not × = uninitialized pointer, does not


Introduction
point to anything specific, just as an ordi- point to anything.
Input
nary variable, when declared, contains an
unknown value.
Arrays

Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action allocate p
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters
int *p;
x ? y ? p ×
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p);
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When a pointer is declared it does not × = uninitialized pointer, does not


Introduction
point to anything specific, just as an ordi- point to anything.
Input
nary variable, when declared, contains an
unknown value.
Arrays

Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action set variables
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters
int *p;
x 1 y 2 p ×
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p);
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

A pointer can be set to point to a variable × = uninitialized pointer, does not


Introduction
by setting it to the variable’s address using point to anything.
Input
the ‘&’ operator.
• = initialized pointer, points to a
Arrays
real variable.
Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action p points to x
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x 1 y 2 p •
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p);
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

A pointer can be set to point to a variable × = uninitialized pointer, does not


Introduction
by setting it to the variable’s address using point to anything.
Input
the ‘&’ operator.
• = initialized pointer, points to a
Arrays
real variable.
Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action output x,y,*p;
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x 1 y 2 p •
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

A pointer’s pointee can be accessed using × = uninitialized pointer, does not


Introduction
the dereferencing operator ‘*’. point to anything.
Input
• = initialized pointer, points to a
Arrays
real variable.
Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action y set to *p
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x 1 y 1 p •
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0;
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

A pointer’s pointee can be accessed using × = uninitialized pointer, does not


Introduction
the dereferencing operator ‘*’. point to anything.
Input
• = initialized pointer, points to a
Arrays
real variable.
Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action output x,y,*p;
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x 1 y 1 p •
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0; 1 1 1
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

A dereferenced pointer, e.g. *p, can be × = uninitialized pointer, does not


Introduction
treated like an ordinary variable, for exam- point to anything.
Input
ple it can occur on either side of an ‘=’.
• = initialized pointer, points to a
Arrays
real variable.
Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action *p set to 0
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x 0 y 1 p •
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0; 1 1 1
printf("%d %d %d\n",x,y,*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

A dereferenced pointer, e.g. *p, can be × = uninitialized pointer, does not


Introduction
treated like an ordinary variable, for exam- point to anything.
Input
ple it can occur on either side of an ‘=’.
• = initialized pointer, points to a
Arrays
real variable.
Strings

Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action output x,y,*p;
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters x 0 y 1 p •
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0; 1 1 1
printf("%d %d %d\n",x,y,*p); 0 1 0
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When used with pointers, ‘*’ has two dif- × = uninitialized pointer, does not
Introduction ferent meanings. point to anything.
Input
In a declaration int *p; * signifies that p
is a pointer to a variable of type int. • = initialized pointer, points to a
Arrays
In an assignment y = *p; * dereferences real variable.
Strings
p, so that we can get to the pointee.
Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action close scope
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0; 1 1 1
printf("%d %d %d\n",x,y,*p); 0 1 0
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
A Simple Example

When used with pointers, ‘*’ has two dif- × = uninitialized pointer, does not
Introduction ferent meanings. point to anything.
Input
In a declaration int *p; * signifies that p
is a pointer to a variable of type int. • = initialized pointer, points to a
Arrays
In an assignment y = *p; * dereferences real variable.
Strings
p, so that we can get to the pointee.
Pointers
Intro
Example
Dereferencing
Pointers & Pointees
Code Action END PROGRAM
Deleting /* Pointer01.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int x, y;
Function Parameters
int *p;
Animation
Exercise P1: x = 1; y = 2; Memory
Exercise P2: p = &x;
Exercises P3-P6 printf("%d %d %d\n",x,y,*p);
Output
y = *p ;
printf("%d %d %d\n",x,y,*p); 1 2 1
*p = 0; 1 1 1
printf("%d %d %d\n",x,y,*p); 0 1 0
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 64 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action start program


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action open scope


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action allocate x, y


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x ? y ?
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action allocate p, q


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers
char pointers
int *p, *q;
x ? y ? p × q×
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action set variables


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers
char pointers
int *p, *q;
x 1 y 2 p × q×
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action p points to x


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 1 y 2 p • q ×
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action q points to y


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 1 y 2 p • q •
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3;
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action output *p,*q


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 1 y 2 p • q •
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action *p set to 3


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 3 y 2 p • q •
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action *q set to 7


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 3 y 7 p • q •
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action *p set to *p * *q


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 21 y 7 p • q •
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7;
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action output *p,*q


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers x 21 y 7 p • q •
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7; *p=21 *q=7
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action close scope


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7; *p=21 *q=7
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Dereferencing

A dereferenced pointer can be used like an ordinary variable, and can participate in
Introduction
arithmetic calculations, for example *p = *p * *q;
Input
In the following, the symbol ‘*’ has 3 different meanings.
In int *p, *q; * signifies that p and q are pointers.
Arrays
In *p = *p * *q; * stands for both dereferencing and multiplication.
Strings

Pointers Code Action END PROGRAM


Intro /* Pointer02.c */
Example
#include <stdio.h>
Dereferencing
Pointers & Pointees int main()
Deleting {
Swapping int x, y;
int pointers
char pointers
int *p, *q;
int & char pointers x = 1; y = 2; Memory
Pointers to Pointers p = &x;
Function Parameters
q = &y;
Animation Output
Exercise P1: printf("*p=%d *q=%d\n",*p,*q);
Exercise P2: *p = 3; *p=1 *q=2
Exercises P3-P6
*q = 7; *p=21 *q=7
*p = *p * *q;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 65 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action start program


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action open scope


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action allocate x, y


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x ? y ?
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action allocate p, q, r


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
int *p, *q, *r;
x ? y ? p × q× r×
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action set variables


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
int *p, *q, *r;
x 1 y 2 p × q× r×
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action p points to x


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 1 y 2 p • q × r×
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action q points to y


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 1 y 2 p • q • r ×
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action r points to y


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 1 y 2 p • q • r •
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}
At this stage pointers q and r point to the same location y. This is an example of pointer aliasing. q and r
are said to be aliased.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action output *p,*q,*r


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 1 y 2 p • q • r •
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}
At this stage pointers q and r point to the same location y. This is an example of pointer aliasing. q and r
are said to be aliased.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action *p set to 3


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 3 y 2 p • q • r •
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action *q set to 7


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 3 y 7 p • q • r •
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action r points to x


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 3 y 7 p • q • r •
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3;
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action output *p,*q,*r


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
x 3 y 7 p • q • r •
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3; *p=3 *q=7 *r=3
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action close scope


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3; *p=3 *q=7 *r=3
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Manipulating Pointers & Pointees
If p points to x, then *p and x refer to the same thing.
Changing the value of x changes the value of *p and vice-versa.
A pointer can be changed to point to any variable at any time.

Code Action END PROGRAM


/* Pointer03.c */
#include <stdio.h>
int main()
{
int x, y;
int *p, *q, *r;
x = 1; y = 2; Memory
p = &x;
q = &y;
Output
r = &y;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r); *p=1 *q=2 *r=2
*p = 3; *p=3 *q=7 *r=3
*q = 7;
r = &x;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 66 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action start program


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action open scope


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action allocate x, y, z, p, q, r


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x = 1; y = 2;
x ? y ? z ? p × q× r×
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action set variables


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x = 1; y = 2;
x 1 y 2 z ? p × q× r×
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action p points to x


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 1 y 2 z ? p • q × r×
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action q points to y


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 1 y 2 z ? p • q • r ×
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action y set to *p


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 1 y 1 z ? p • q • r ×
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action *p set to 0


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 0 y 1 z ? p • q • r ×
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action r points to z


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 0 y 1 z ? p • q • r •
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action *r set to 7


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 0 y 1 z 7 p • q • r •
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z;
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action output *p,*q,*r


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 0 y 1 z 7 p • q • r •
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z; *p=0 *q=1 *r=7
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action q set to NULL


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x 0 y 1 z 7 p • q ×r •
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z; *p=0 *q=1 *r=7
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action close scope


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z; *p=0 *q=1 *r=7
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
Deleting a pointer
A pointer is set to point to a variable, which may or
may not be initialized.
Here r points to z which is not initialized. Trying to
access the pointee of r (e.g. y =*r) would yield a
meaningless result.
A pointer can be deleted by setting it to NULL. This
breaks the link between pointer and pointee.
Dereferencing a NULL pointer is an error, as the
pointer does not point to a valid location. If you put
the statement y= *q at this point you would get an
error on running the program.

Code Action END PROGRAM


/* Pointer04.c */
#include <stdio.h>
int main()
{
int x, y, z, *p, *q, *r;
x = 1; y = 2;
p = &x; Memory
q = &y;
y = *p;
Output
*p = 0;
r = &z; *p=0 *q=1 *r=7
*r = 7;
printf("*p=%d *q=%d *r=%d\n",*p,*q,*r);
q = NULL;
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright STEP PAGE PREV – 67 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action start program
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
Swapping
int x, y, *p, *q, *r;
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q;
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action open scope
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
Swapping
int x, y, *p, *q, *r;
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q;
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action allocate x, y, p, q, r
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping
int pointers x = 1; y = 7;
x ? y ? p × q× r×
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q;
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action set variables
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping
int pointers x = 1; y = 7;
x 1 y 7 p × q× r×
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q;
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action p points to x
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q × r×
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q;
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action q points to y
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q • r ×
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q;
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action output *p,*q
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q • r ×
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action r set to p
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q • r •
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action p set to q
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q • r •
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action q set to r
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q • r •
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r;
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action output *p,*q
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
int x, y, *p, *q, *r;
Swapping x 1 y 7 p • q • r •
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r; *p=7 *q=1
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action close scope
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
Swapping
int x, y, *p, *q, *r;
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r; *p=7 *q=1
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Swapping Pointers

Introduction Pointers p and q are swapped using a temporary pointer r.


Input Before the swap, p points to x and q to y.
Arrays After the swap, p points to y and q to x.
Strings
Code Action END PROGRAM
Pointers
/* Pointer05.c */
Intro
Example #include <stdio.h>
Dereferencing int main()
Pointers & Pointees {
Deleting
Swapping
int x, y, *p, *q, *r;
int pointers x = 1; y = 7;
char pointers p = &x; Memory
int & char pointers
Pointers to Pointers
q = &y;
Function Parameters printf("*p=%d *q=%d\n",*p,*q);
Output
Animation r = p;
Exercise P1:
p = q; *p=1 *q=7
Exercise P2:
Exercises P3-P6 q = r; *p=7 *q=1
printf("*p=%d *q=%d\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 68 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action start program
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action open scope
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action allocate p, z, y, x
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters
x = 1; y = 7; z = 3;
x ? y ? z ? p ×
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action set variables
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters
x = 1; y = 7; z = 3;
x 1 y 7 z 3 p ×
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action p points to x
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters x 1 y 7 z 3 p •
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action output *p
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters x 1 y 7 z 3 p •
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action increment p
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters x 1 y 7 z 3 p •
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action output *p
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters x 1 y 7 z 3 p •
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p); *p=7
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action increment p
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters x 1 y 7 z 3 p •
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p); *p=7
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action output *p
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters x 1 y 7 z 3 p •
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p); *p=7
} *p=3

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action close scope
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p); *p=7
} *p=3

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to int

Ordinary arithmetic operations, for example increment & decrement can be applied
Introduction
to pointers.
Input
However the size of the pointee determines how a pointer is changed.
For example, incrementing a pointer to int would change it, so that it points to the
Arrays
next int in memory.
Strings
We have displayed our variables in the same order they would occur in memory.
Pointers
Incrementing a pointer changes it to point to the next int.
Intro
Example More details are given in the Section Pointers & Arrays
Dereferencing
Pointers & Pointees Code Action END PROGRAM
Deleting /* Pointer06.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int *p, z, y, x;
Function Parameters
x = 1; y = 7; z = 3;
Animation
Exercise P1: p = &x; Memory
Exercise P2: printf("*p=%d\n",*p);
Exercises P3-P6 p++;
Output
printf("*p=%d\n",*p);
p++; *p=1
printf("*p=%d\n",*p); *p=7
} *p=3

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 69 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action start program
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
Pointers to Pointers
char *p, z, y, x;
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++;
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action open scope
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
Pointers to Pointers
char *p, z, y, x;
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++;
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action allocate *p, z, y, x
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers
Function Parameters x = ’a’; y = ’b’; z = ’c’;
x ? y ? z ? p ×
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++;
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action set variables
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers
Function Parameters x = ’a’; y = ’b’; z = ’c’;
x ’a’ y ’b’ z ’c’ p ×
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++;
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action p set to address of x
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers x ’a’ y ’b’ z ’c’ p •
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++;
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action output *p
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers x ’a’ y ’b’ z ’c’ p •
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action increment p
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers x ’a’ y ’b’ z ’c’ p •
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action output *p
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers x ’a’ y ’b’ z ’c’ p •
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p); *p=b
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action increment p
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers x ’a’ y ’b’ z ’c’ p •
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p); *p=b
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action output *p
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
char *p, z, y, x;
Pointers to Pointers x ’a’ y ’b’ z ’c’ p •
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p); *p=b
} *p=c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action close scope
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
Pointers to Pointers
char *p, z, y, x;
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p); *p=b
} *p=c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Incrementing pointers to char

Introduction Incrementing a pointer to char changes it by an amount equal to the size of char.
Input While this might be obvious, we must remember that chars and ints have different
Arrays sizes.
Strings A char occupies one byte in memory, while an int typically occupies 4 bytes.
Pointers This is usually of no concern to us except when we mix up chars and ints, as
Intro described on the next page.
Example
Dereferencing Code Action END PROGRAM
Pointers & Pointees /* Pointer07.c */
Deleting
Swapping
#include <stdio.h>
int pointers int main()
char pointers {
int & char pointers
Pointers to Pointers
char *p, z, y, x;
Function Parameters x = ’a’; y = ’b’; z = ’c’;
Animation p = &x; Memory
Exercise P1:
printf("*p=%c\n",*p);
Exercise P2:
Exercises P3-P6 p++;
Output
printf("*p=%c\n",*p);
p++; *p=a
printf("*p=%c\n",*p); *p=b
} *p=c

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 70 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action start program


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action open scope


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action allocate p


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
p ×
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action allocate q


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
p × q×
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action allocate z, y, x, w, v


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
v ? w ? x ? y ? z ? p × q×
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action set variables


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p × q×
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action p points to v


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p • q ×
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action q points to v


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p • q •
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q);
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action output *p,*q


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p • q •
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q); *p=a *q=a
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action increment p


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p • q •
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q); *p=a *q=a
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action increment q


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p • q •
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q); *p=a *q=a
p++;
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action output *p,*q


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
v ’a’ w ’b’ x ’c’ y ’d’ z ’e’ p • q •
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q); *p=a *q=a
p++; *p=b *q=e
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action close scope


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q); *p=a *q=a
p++; *p=b *q=e
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
Incrementing pointers of mixed types
Typically, a char variable is stored in one byte and an int in 4 bytes.
Incrementing a char pointer will increase it to point to the next char.
Incrementing an int pointer will increase it to point 4 bytes away.
In this example, we (incorrectly) use an int pointer to a char to provide an example of the above
observations.
Note that by declaring variables in the order z, y, x, w, v we cause them to be stored in memory in order
v, w, x, y, z.

Code Action END PROGRAM


/* Pointer08.c */
#include <stdio.h>
int main()
{
char *p;
int *q;
char z, y, x, w, v; Memory
v=’a’; w=’b’; x=’c’; y=’d’; z=’e’;
p = &v;
Output
q = &v;
printf("*p=%c *q=%c\n",*p,*q); *p=a *q=a
p++; *p=b *q=e
q++;
printf("*p=%c *q=%c\n",*p,*q);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 71 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action start program
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action open scope
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action allocate s, t, u
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
s × t× u×
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action allocate x, y
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
x ? y ? s × t× u×
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action set variables
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
x 3 y -7 s × t× u×
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action t points to x
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters x 3 y -7 s × t • u ×
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action u points to y
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters x 3 y -7 s × t • u •
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action s points to t
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters x 3 y -7 s • t • u •
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s);
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action output *t,*u,**s;
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters x 3 y -7 s • t • u •
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s); 3 -7 3
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action s points to u
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters x 3 y -7 s • t • u •
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s); 3 -7 3
s = &u;
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action output *t,*u,**s;
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters x 3 y -7 s • t • u •
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s); 3 -7 3
s = &u; 3 -7 -7
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action close scope
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s); 3 -7 3
s = &u; 3 -7 -7
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers to Pointers

Pointers can point to other pointers.


Introduction
In this example, int **s declares s to be a
Input
pointer to a pointer to int.
s can only point to another pointer.
Arrays
s has to be dereferenced twice to get to
Strings
the ultimate pointee.
Pointers
Each dereferencing corresponds to travel-
Intro
Example ing over an arrow.
Dereferencing
Pointers & Pointees Code Action END PROGRAM
Deleting /* Pointer09.c */
Swapping #include <stdio.h>
int pointers
char pointers
int main()
int & char pointers {
Pointers to Pointers int **s, *t, *u;
Function Parameters
int x, y;
Animation
Exercise P1: x = 3; y = -7; Memory
Exercise P2: t = &x;
Exercises P3-P6 u = &y;
Output
s = &t;
printf("%d %d %d\n",*t,*u,**s); 3 -7 3
s = &u; 3 -7 -7
printf("%d %d %d\n",*t,*u,**s);
}

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 72 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers and Function Parameters

/* func02.c */
We have now learned enough about
#include <stdio.h>
Introduction pointers to be able to understand their
void max2(int a,int b,int *c)
Input use in functions.
{
Arrays
The function max2 takes the value of a if (a>b)
Strings
and the value of b, and puts the larger *c=a;
Pointers
value in c. Since the value of c is being else
Intro
Example changed, we need to supply the address *c=b;
Dereferencing of c. Unlike max presented earlier, max2 }
Pointers & Pointees int main()
does not return a value (it supplies its
Deleting
Swapping
answer by modifying c). max2 therefore {
int pointers has type void. int x, y, z;
char pointers scanf("%d",&x);
int & char pointers a, b and c in the function are replaced
scanf("%d",&y);
Pointers to Pointers with x, y and z from the main program.
Function Parameters max2(x,y,&z);
Animation In the function we declare the third vari- printf("%d\n",z);
Exercise P1:
able (or parameter) as int *c, that is a }
Exercise P2:
Exercises P3-P6 pointer to c.
In the main program, the third parameter
is &x, that is the address of x.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 73 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers and Function Parameters

/* func02.c */
We have now learned enough about
#include <stdio.h>
Introduction pointers to be able to understand their
void max2(int a,int b,int *c)
Input use in functions.
{
Arrays
The function max2 takes the value of a if (a>b)
Strings
and the value of b, and puts the larger *c=a;
Pointers
value in c. Since the value of c is being else
Intro
Example changed, we need to supply the address *c=b;
Dereferencing of c. Unlike max presented earlier, max2 }
Pointers & Pointees int main()
does not return a value (it supplies its
Deleting
Swapping
answer by modifying c). max2 therefore {
int pointers has type void. int x, y, z;
char pointers scanf("%d",&x);
int & char pointers a, b and c in the function are replaced
scanf("%d",&y);
Pointers to Pointers with x, y and z from the main program.
Function Parameters max2(x,y,&z);
Animation In the function we declare the third vari- printf("%d\n",z);
Exercise P1:
able (or parameter) as int *c, that is a }
Exercise P2:
Exercises P3-P6 pointer to c.
In the main program, the third parameter
is &x, that is the address of x.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 73 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers and Function Parameters

/* func02.c */
We have now learned enough about
#include <stdio.h>
Introduction pointers to be able to understand their
void max2(int a,int b,int *c)
Input use in functions.
{
Arrays
The function max2 takes the value of a if (a>b)
Strings
and the value of b, and puts the larger *c=a;
Pointers
value in c. Since the value of c is being else
Intro
Example changed, we need to supply the address *c=b;
Dereferencing of c. Unlike max presented earlier, max2 }
Pointers & Pointees int main()
does not return a value (it supplies its
Deleting
Swapping
answer by modifying c). max2 therefore {
int pointers has type void. int x, y, z;
char pointers scanf("%d",&x);
int & char pointers a, b and c in the function are replaced
scanf("%d",&y);
Pointers to Pointers with x, y and z from the main program.
Function Parameters max2(x,y,&z);
Animation In the function we declare the third vari- printf("%d\n",z);
Exercise P1:
able (or parameter) as int *c, that is a }
Exercise P2:
Exercises P3-P6 pointer to c.
In the main program, the third parameter
is &x, that is the address of x.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 73 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers and Function Parameters

/* func02.c */
We have now learned enough about
#include <stdio.h>
Introduction pointers to be able to understand their
void max2(int a,int b,int *c)
Input use in functions.
{
Arrays
The function max2 takes the value of a if (a>b)
Strings
and the value of b, and puts the larger *c=a;
Pointers
value in c. Since the value of c is being else
Intro
Example changed, we need to supply the address *c=b;
Dereferencing of c. Unlike max presented earlier, max2 }
Pointers & Pointees int main()
does not return a value (it supplies its
Deleting
Swapping
answer by modifying c). max2 therefore {
int pointers has type void. int x, y, z;
char pointers scanf("%d",&x);
int & char pointers a, b and c in the function are replaced
scanf("%d",&y);
Pointers to Pointers with x, y and z from the main program.
Function Parameters max2(x,y,&z);
Animation In the function we declare the third vari- printf("%d\n",z);
Exercise P1:
able (or parameter) as int *c, that is a }
Exercise P2:
Exercises P3-P6 pointer to c.
In the main program, the third parameter
is &x, that is the address of x.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 73 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Pointers and Function Parameters

/* func02.c */
We have now learned enough about
#include <stdio.h>
Introduction pointers to be able to understand their
void max2(int a,int b,int *c)
Input use in functions.
{
Arrays
The function max2 takes the value of a if (a>b)
Strings
and the value of b, and puts the larger *c=a;
Pointers
value in c. Since the value of c is being else
Intro
Example changed, we need to supply the address *c=b;
Dereferencing of c. Unlike max presented earlier, max2 }
Pointers & Pointees int main()
does not return a value (it supplies its
Deleting
Swapping
answer by modifying c). max2 therefore {
int pointers has type void. int x, y, z;
char pointers scanf("%d",&x);
int & char pointers a, b and c in the function are replaced
scanf("%d",&y);
Pointers to Pointers with x, y and z from the main program.
Function Parameters max2(x,y,&z);
Animation In the function we declare the third vari- printf("%d\n",z);
Exercise P1:
able (or parameter) as int *c, that is a }
Exercise P2:
Exercises P3-P6 pointer to c.
In the main program, the third parameter
is &x, that is the address of x.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 73 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action start program
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b;
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

Program execution always starts in main.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action open scope
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b;
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

Program execution always starts in main.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action allocate x, y, z
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x ? y ? z ?
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

Program execution always starts in main.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action input x;
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y ? z ?
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

Program execution always starts in main.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action input y;
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z ?
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

Program execution always starts in main.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action call max2(x,y,&z);
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z ?
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

max2 is called.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action in max2: open scope
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z ?
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

A separate frame is opened for max2’s variables.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action copy parameters
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z ? a 3 b 9 c •
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

The values of x and y are copied into a and b respectively.


The address of z is copied into c.
c in the function points to z in the main program.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action if (a > b)
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z ? a 3 b 9 c •
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

The max of a and b is computed.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action *c set to b
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z 9 a 3 b 9 c •
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

This is written into the location pointed to by c.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action close scope
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z 9
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}

The function ends.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action back in main program: output z
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b; x 3 y 9 z 9
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}
9
We return to the line after the call to max2.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action close scope
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b;
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}
9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
Function Call Animation
In this animation, note carefully how the main program copies the int values x, y into a and b respectively.
It copies the address &x into c, which is thus a pointer to x. max2 computes the maximum and writes it
at the address c which is x.
Code Action END PROGRAM
/* func02.c */
#include <stdio.h>
void max2(int a,int b,int *c)
{
if (a>b)
*c=a;
else
*c=b;
}
main program function max2
int main()
{
int x, y, z; Input
scanf("%d",&x); 3
scanf("%d",&y); 9
max2(x,y,&z);
printf("%d\n",z); Output
}
9

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 74 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings

Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings

Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings

Pointers
y ? z ? p × q× r× s×
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings

Pointers
y 7 z -3 p × q× r× s×
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q × r× s×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r × s×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r • s ×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=7 *q=7 *r=-3 *s=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=7 *q=7 *r=-3 *s=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=7 *q=7 *r=-3 *s=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings
y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=7 *q=7 *r=-3 *s=-3
char pointers *p=-3 *q=7 *r=7 *s=-3
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P1:

Introduction Write a program with two vari-


Input ables and and four pointers
Arrays whose behavior is as shown.
Strings

Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=7 *q=7 *r=-3 *s=-3
char pointers *p=-3 *q=7 *r=7 *s=-3
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 75 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
Pointers
x ? y ? z ? p × q× r× s×
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
Pointers
x 1 y 7 z -3 p × q× r× s×
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q × r× s×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r × s×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s ×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s ×
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
x 1 y 7 z -3 p • q • r • s •
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers *p=7 *q=-3 *r=1
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercise P2:

Introduction Write a program that carries


Input out a 3-way swap as shown.
Arrays This is a extension of the swap
Strings program Pointers05.c
Pointers
Intro
Memory
Example
Dereferencing
Pointers & Pointees Output
Deleting
Swapping
int pointers
*p=1 *q=7 *r=-3
char pointers *p=7 *q=-3 *r=1
int & char pointers
Pointers to Pointers
Function Parameters
Animation
Exercise P1:
Exercise P2:
Exercises P3-P6

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 76 / 77
C Programming:
Memory & Pointers
An Algopath eBook
Exercises P3-P6

Introduction
Exercise P3: In Pointer04.c print out the value of *r before its pointee has been
Input initialized. What is the value? Execute the program several times. What do you
Arrays observe?
Strings
Exercise P4: In Pointer04.c print out the value of *q after q is set to NULL. What
Pointers
Intro
do you observe?
Example
Dereferencing Exercise P5: What is really stored in a pointer? This can be established by
Pointers & Pointees printing out the contents of a pointer (not the pointee) with printf(”%p\n”, r);
Deleting
which will print out the address stored in hexadecimal form. Try this out on
Swapping
int pointers
Pointer04.c
char pointers
int & char pointers Exercise P6: The C function sizeof() will return the size (in bytes) of a variable on
Pointers to Pointers your specific computer system. For example, if c is declared to be char, sizeof(c)
Function Parameters
will return 1. Write a program that declares variables (and pointers to these
Animation
Exercise P1:
variables) of type char, int, float and double. Obtain their sizes, using sizeof().
Exercise P2: Verify that incrementing the pointers causes an increase equal to the size of the
Exercises P3-P6 corresponding variables.

c 2008 Algopath LLC. All Rights Reserved.


Copyright STEP PAGE PREV – 77 / 77

You might also like