Professional Documents
Culture Documents
Shahid Bokhari
An Algopath eBook
www.algopath.com
ISBN 978-0-9802341-1-4
2008
Adobe
R & Adobe
R Reader
R are trademarks of Adobe Systems Inc.
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
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.
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.
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.
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.
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.
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
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
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
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
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
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
Input
Arrays
Strings
Pointers
Input
Arrays
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
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
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.”
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.”
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.”
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.”
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.
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.
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
Pointers
Pointers
Pointers
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Input
Arrays
Strings
Pointers
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
4 2.300000
4 2.300000
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)
/* 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);
}
/* 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);
}
/* 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);
}
/* 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);
}
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
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
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
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
8 64
8 64
8 64
8 64
8 64
8 64
8 64
8 64
-3 9
8 64
-3 9
8 64
-3 9
8 64
-3 9
8 64
-3 9
8 64
-3 9
8 64
-3 9
8 64
-3 9
11 121
8 64
-3 9
11 121
8 64
-3 9
11 121
8 64
-3 9
11 121
8 64
-3 9
11 121
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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.
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.
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
Output
the max is 9
Output
the max is 9
Output
the max is 9
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.
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
/* 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
/* 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
/* 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
/* 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
/* 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
/* 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
/* 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
/* 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
/* 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
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
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
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 }
Memory
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 }
Memory
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 }
Memory
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 }
Memory
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 }
Memory
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 }
Memory
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 }
Memory
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 }
Memory
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 }
Memory
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Strings
Pointers Memory
Strings
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Pointers Memory
Strings
Pointers Memory
Strings
Pointers Memory
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
Strings Memory
Pointers
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Strings
Pointers
i 0 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0
Memory
Strings
Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0
Memory
Strings
Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0
Memory
Strings t ?
Pointers
i 1 a[0] 5 a[1] 1 a[2] 2 a[3] 3 a[4] 4 a[5] 0
Memory
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
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
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
Strings
Pointers
i 1 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0
Memory
Strings
Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0
Memory
Strings
Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0
Memory
Strings t ?
Pointers
i 2 a[0] 5 a[1] 4 a[2] 2 a[3] 3 a[4] 1 a[5] 0
Memory
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
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
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
Strings
Pointers
i 2 a[0] 5 a[1] 4 a[2] 3 a[3] 2 a[4] 1 a[5] 0
Memory
Strings
Pointers
i 3 a[0] 5 a[1] 4 a[2] 3 a[3] 2 a[4] 1 a[5] 0
Memory
Strings
Pointers
Memory
Strings
Pointers
Memory
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Memory
Memory
Memory
Memory
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
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
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
Memory
Memory
Memory
Memory
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
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
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
Memory
Memory
Memory
Memory
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
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
Pointers
Memory
Pointers
Memory
Memory
Memory
i ?
Memory
Memory
Memory
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Memory
Memory
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.
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.
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.
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.
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.
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.
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.
Strings
Pointers
Memory
Strings
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Set a[0] to 9
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Set a[1] to 9
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Set a[2] to 9
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
Set a[3] to 9
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
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.
Pointers
Memory
Pointers
Memory
Pointers
Memory
Pointers
Memory
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.
Pointers
Memory
The for loop executes for 6 steps, rather than the expected 5. This is because both
c and b have been overwritten.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Memory
Memory
i ?
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?
Memory
i 0 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?
Memory
i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?
Memory
i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?
Memory
i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?
Memory
i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ? c[2] ?
Memory
i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?
Memory
i 1 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?
Memory
i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?
Memory
i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?
Memory
i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?
Memory
i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ?
Memory
i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ’c’
Memory
i 2 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ’c’
Memory
i 3 b[0] ’a’ b[1] ’a’ b[2] ’a’ c[0] ’a’ c[1] ’b’ c[2] ’c’
Memory
Memory
Memory
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
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
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
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
Memory
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
Memory
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
Memory
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
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
Memory
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
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
Memory
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
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
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
Memory
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
Memory
Strings are read in with scanf using the ”%s” format specification. An ampersand
(&) is not used, as shown above.
Memory
Strings are read in with scanf using the ”%s” format specification. An ampersand
(&) is not used, as shown above.
Memory
Strings are read in with scanf using the ”%s” format specification. An ampersand
(&) is not used, as shown above.
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.
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.
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.
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.
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.
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.
Memory
However any overwritten parts will be ignored, since the string, by definition, starts
at a[0] and extends up to the first NULL.
Memory
However any overwritten parts will be ignored, since the string, by definition, starts
at a[0] and extends up to the first NULL.
Memory
Memory
i ?
Memory
Memory
Memory
Memory
i ? a[0] ’h’ a[1] ’e’ a[2] ’l’ a[3] ’l’ a[4] ’o’ a[5] ’\0’
Memory
i ? a[0] ’h’ a[1] ’e’ a[2] ’l’ a[3] ’l’ a[4] ’o’ a[5] ’\0’
Memory
i ? a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i ? a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 0 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 0 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 1 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 1 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 2 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 2 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 3 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 3 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 4 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 4 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 5 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 5 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
i 6 a[0] ’b’ a[1] ’y’ a[2] ’e’ a[3] ’\0’ a[4] ’o’ a[5] ’\0’
Memory
Memory
Memory
Memory
Memory
a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? b[4] ?
Memory
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
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
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
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
Memory
Memory
Memory
Memory
b[0] ? b[1] ? b[2] ? b[3] ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ?
Memory
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
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
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
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
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
Memory
Memory
Memory
Memory
a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? a[5] ? a[6] ? a[7] ? a[8] ? b[0] ? b[1] ?
Memory
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
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
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
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
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
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
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
Memory
Memory
Memory
Memory
a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? b[4] ?
Memory
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
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
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
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
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
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
Memory
Memory
Memory
Strings can be modified by accessing the corresponding char array. If you are not careful, you may get
strange results.
Memory
Strings can be modified by accessing the corresponding char array. If you are not careful, you may get
strange results.
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.
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.
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.
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.
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.
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.
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”.
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’.
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:
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
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:
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
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:
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:
Memory
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.
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);
}
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
Introduction
Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays
Introduction
Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays
Introduction
Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays
Introduction
Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays
Introduction
Input
Pointers are a special kind of variable that
point or refer to a variable.
Arrays
Introduction
Input
Pointers are a special kind of variable that
point or refer to a variable.
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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
}
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
}
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
}
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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
}
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
}
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
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
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
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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
}
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
}
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
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
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
/* 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.
/* 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.
/* 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.
/* 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.
/* 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.
max2 is called.
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
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
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
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
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
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.