Professional Documents
Culture Documents
Bokhari C3
Bokhari C3
Shahid Bokhari
An Algopath eBook
www.algopath.com
ISBN 978-0-9802341-2-1
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
Dynamic Memory
Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.
Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.
Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.
Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.
Introduction This eBook deals with the basic concepts of memory and
Quick Start
Prerequisites
pointer usage in the C programming language. We presume
This eBook only an elementary knowledge of C programming.
Audience
Exercises
Basically all that is required is the ability to use an editor
Contact Us to enter (and possibly change) a program and knowledge of
Getting Started the commands to compile and execute a program.
A tiny program
Animation Familiarity with an integrated development environment
Initializing (IDE) that combines these steps in a graphical interface
An Experiment
A Big Jump
is a convenient alternative.
Pointers & Arrays While there are many fine textbooks that introduce the C
Dynamic Memory
language, several important concepts, in particular memory
and pointer behavior, cannot be adequately covered in a
traditional paper textbook.
At the same time, a clear understanding of these concepts
is necessary for a programmer to make progress beyond the
elementary level.
Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook
Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook
Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook
Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook
Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
This eBook
Introduction
Quick Start This eBook uses powerful techniques for representing, in
Prerequisites a visual form, the operations that take place when a C
This eBook
Audience
program is being executed.
Exercises
Contact Us
These operations are complex and require large numbers
Getting Started of diagrams to explain clearly. This is clearly impossi-
A tiny program ble in a traditional printed textbook or in a classroom
Animation
lecture.
Initializing
An Experiment
The text processing and animation techniques of this
A Big Jump
Algopath eBook allow us to use hundreds of individual
Pointers & Arrays
snapshots to represent the progress of a program.
Dynamic Memory
This permits the student to carefully study the behavior
of a program at his or her own pace without being limited
by the pages of a textbook or the time allotted for a
lecture.
This eBook includes about 100 programs that are ex-
plained using several thousand snapshots. The student
is free to examine and re-examine the execution of these
example programs as many times and at whatever pace
he or she desires.
This Algopath eBook thus provides an exciting new tool
for learning the concepts of programming.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 6 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Audience
Dynamic Memory
Dynamic Memory
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
Dynamic Memory
Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”
Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”
Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”
Introduction This Algopath eBook has many powerful features that dis-
Quick Start
Prerequisites
tinguish it from an ordinary eBook:
This eBook
Audience 1. It permits the user to clearly visualize the effect of each
Exercises line in a program.
Contact Us
Getting Started 2. The size of the window can be controlled by the user.
A tiny program
You can even the use the full screen mode of Reader
Animation
Initializing (use the CTRL-L and ESC keys to toggle to and from
An Experiment full-screen mode) to make the maximum use of your
A Big Jump display screen.
Pointers & Arrays
3. In some situations you may wish to use a smaller win-
Dynamic Memory
dow for the eBook so that you can enter, edit, compile
and execute your own programs using your own edi-
tor, compiler or IDE side-by-side with the eBook. This
permits you to try out your own programs, that could
be variants or modifications of those presented in the
eBook.
4. At some points in the eBook we specifically ask you to
try out such modifications–providing you with valuable
opportunities to “learn by doing.”
Dynamic Memory As you move through the lines of this program using the space bar, scroll wheel or
the step arrows of eBook, you will see how memory is allocated and the allocated
during the lifetime of the program and. You will also see the output, which is a
single line.
Dynamic Memory The concept of scope is very important. Whenever an opening brace “{” (also
called “curly bracket”) is encountered, a new scope is opened. The eBook indicates
a scope with a rectangular frame in which variables are allocated as desired.
Dynamic Memory x, y & z are allocated, that is space is set aside for them in memory. Just after a
variable is allocated, its value is an unknown (indicated by ?).
Dynamic Memory
Introduction
Dynamic Memory
Dynamic Memory
Memory
p1, p2 are pointers to int–they can be used to point to int array elements as well.
Dynamic Memory
Memory
p1, p2 are pointers to int–they can be used to point to int array elements as well.
Dynamic Memory
Memory
p1, p2 are pointers to int–they can be used to point to int array elements as well.
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Incrementing p1 causes it to point to the element that follows x[0], i.e. x[1].
Dynamic Memory
Memory
Incrementing p2 causes it to point to the element that follows x[3], i.e. x[4].
Dynamic Memory
Memory
Code Action p2 = x;
Introduction
/* ptrArray01.c */
Pointers & Arrays
#include <stdio.h>
Intro
#define M 5 Input
Array Addresses
Indexes & Pointers–I int main() 2 9 1 7 8
Indexes & Pointers–II {
Indexes & Pointers–III
int x[M], *p1, *p2; Output
Pointer types
Pointer experiment getIntArray(x,M); 8
Using Pointers p1 = &x[0];
Array bounds
p2 = &x[3];
Even elements
Copying p1++;
Reverse Copy p2++;
Flip printf("%d\n",*p2);
int to char
String Length
p2 = x;
Search printf("%d\n",*p2);
Binary Search }
Exercises PA1-PA5
Exercises PA6-PA8
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
In C, the name of an array is a pointer and points to the first element of the array.
Dynamic Memory
Memory
In C, the name of an array is a pointer and points to the first element of the array.
Dynamic Memory
Memory
We can access the elements of array d using array indexing, that is d[0], d[1], . . .
Dynamic Memory
Memory
We can access the elements of array d using array indexing, that is d[0], d[1], . . .
Dynamic Memory
Memory
We can access the elements of array d using array indexing, that is d[0], d[1], . . .
Dynamic Memory
Memory
We can access the elements of array d using array indexing, that is d[0], d[1], . . .
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
This for loop prints out array elements in a familiar way that we have used in prior
examples.
Dynamic Memory
Memory
The same result can be obtained by setting ptr to point to the start of the array d
and then printing *ptr, incrementing ptr, and so on.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
C always increments a pointer by an amount appropriate for the size of the pointee.
The programmer does not have to be concerned.
Dynamic Memory
Memory
Notice that at the end of the while loop, ptr points to i, which happens to be at
the location after d[3].
Dynamic Memory
Memory
The while loop terminates when ptr points beyond d[3], so this location is never
accessed.
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
ptr × d[0] ? d[1] ? d[2] ? d[3] ? i ?
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i ?
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3
Memory
Dynamic Memory
ptr × d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
ptr • d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Dynamic Memory
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] ? d[1] ? d[2] ? d[3] ? i ?
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i ?
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 0
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 1
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 2
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 3
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
d[0] 7 d[1] 8 d[2] 1 d[3] -6 i 4
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
Dynamic Memory
Memory
Since d+i and &d[i] are equivalent, dereferencing (d+i) thus: *(d+i) gives us
*&d[i]=d[i], since * (“contents of”) and & (“address of”) cancel each other.
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
/* ptrArray05.c */
Each variable type (e.g. char, int, float, . . . )
Introduction
#include <stdio.h>
has its own pointer type.
Pointers & Arrays
int main()
Intro The behavior of a pointer depends on its type. {
Array Addresses char c[9] , *pc;
Indexes & Pointers–I For example, chars are stored in one byte each. int *pi, i;
Indexes & Pointers–II When a pointer of type char is incremented, the scanf("%s",c);
Indexes & Pointers–III
Pointer types
contents change by 1. pi = c;
Pointer experiment pc = c;
An int is typically stored in 4 bytes. Increment-
Using Pointers
Array bounds ing a pointer of type int changes the contents for(i=0; i<8; i++)
Even elements by 4, so that it points to the next int variable. printf("%c\n", *pc++);
Copying for(i=0; i<2; i++)
Reverse Copy The programmer has to be aware of this distinc- printf("%c\n", *pi++);
Flip tion.
int to char }
String Length In ptrArray05.c we have a string that is, as you
Search
Binary Search
will recall, stored in an array of char.
Exercises PA1-PA5
Exercises PA6-PA8
We can step through this array of char correctly
using a char pointer.
Dynamic Memory
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc × pi × i ?
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc × pi • i ?
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i ?
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 2
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 2
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 3
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 3
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 4
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 4
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 5
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 5
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 6
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 6
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 7
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 7
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 8
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 0
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 1
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
c[0] ’s’ c[1] ’t’ c[2] ’r’ c[3] ’a’ c[4] ’n’ c[5] ’g’ c[6] ’e’ c[7] ’r’ c[8] ’\0’ pc • pi • i 2
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
Memory
You may get a warning about incorrect pointer usage when compiling this program, but it will run.
⇐ Menu omitted to save space on this and some later pages.
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action start program
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action allocate i, d[6], ptr, tmp
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action input d[ ];
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action ptr set to d
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action open scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action tmp set to ptr + i
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action *tmp += 1;
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action for loop
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action close scope
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
Pointers can be used to manipulate arrays in the same way that ordinary indexing
Introduction
(e.g. d[i]) is used. In this example, we increment each element of the array d.
Pointers & Arrays
Intro
Code Action END PROGRAM
Array Addresses /* ptrArray06.c */
Indexes & Pointers–I #include <stdio.h>
Indexes & Pointers–II
#define MAX 6 Input
Indexes & Pointers–III
Pointer types int main() 7 8 1 -6 9 4
Pointer experiment {
Using Pointers
int i, d[MAX], *ptr, *tmp;
Array bounds
Even elements getIntArray(d,MAX);
Copying ptr = d;
Reverse Copy for(i=0; i<MAX; i++)
Flip
int to char
{
String Length tmp = ptr + i;
Search *tmp += 1;
Binary Search
Exercises PA1-PA5
}
Exercises PA6-PA8 }
Dynamic Memory
Memory
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
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
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
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action allocate a[8], pH, pL, t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action input a[ ];
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action pL set to a
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action pH set to &a[8-1]
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action open scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action t set to *pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pH set to *pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action decrement pH
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action *pL set to t
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action increment pL
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action while( pL < a+8/2 )
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action close scope
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Reversing an Array in Place
An array can be reversed using a temporary location (t) and two pointers (pH, pL). The dereferencing and
incrementing of pointers could be done in one line, but would result in a less useful animation.
Code Action END PROGRAM
/* ptrArray11.c */
#include <stdio.h>
#define M 8 Input
int main() 4 1 -3 7 5 3 9 6
{
int a[M], *pH, *pL, t;
getIntArray(a,M);
pL = a;
pH = &a[M-1];
while( pL < a+M/2 )
{
t = *pH;
*pH = *pL;
pH--;
*pL = t;
pL++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 29 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action start program
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action allocate pC , dC , c[3]
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action *pC set to (char)*pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pC
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action *pC set to (char)*pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pC
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action open scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action *pC set to (char)*pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pI
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action increment pC
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action while (pI < i+3)
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
pI • i[0] 112 i[1] 90 i[2] 51 dI ? c[0] ’p’ c[1] ’Z’ c[2] ’3’ dC ? pC •
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action close scope
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Copying ints to chars
An array of ints is copied into an array of chars. This is only feasible if the ints are small enough to fit in
the chars. In this example, the ints store ASCII codes for ’p’, ’Z’ and ’3’. The ints must be ‘cast’ into
chars. Dummy variables dI, dC have been declared to show you where pI, pC point at the end of the loop.
Code Action END PROGRAM
/* ptrArray12.c */
#include <stdio.h>
#define M 3 Input
int main() 112 90 51 101
{
char *pC , dC , c[M]; Output
int dI , i[M], *pI ;
getIntArray(i,M);
pI = i;
pC = c;
while (pI < i+M)
{
*pC = (char)*pI;
pI++;
pC++;
}
}
Memory
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 30 / 54
Computing the Length of a String
The length of a string can be computed by searching for the NULL or ‘\0’ that marks the end of the string.
Code Action start program
/* ptrArray13.c */
#include <stdio.h>
#define L 8 Input
int main() Slowly
{
char c[L] , *pC ; Output
int len;
scanf("%s",c);
pC = c;
len = 0;
while(*pC != NULL)
{
len++;
pC++;
}
printf("%d\n", len);
}
Memory
Memory
Memory
Memory
len ? pC × c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len ? pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 0 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 0 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 0 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 1 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 2 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 3 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 4 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 5 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
len 6 pC • c[0] ’S’ c[1] ’l’ c[2] ’o’ c[3] ’w’ c[4] ’l’ c[5] ’y’ c[6] ’\0’ c[7] ?
Memory
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
Lo × d[0] ? d[1] ? d[2] ? d[3] ? d[4] ? d[5] ? d[6] ? d[7] ? mid × Hi × val ?
Memory
Lo × d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi × val ?
Memory
Lo × d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi × val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi × val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid × Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Lo • d[0] -3 d[1] -2 d[2] 0 d[3] 1 d[4] 2 d[5] 5 d[6] 9 d[7] 12 mid • Hi • val 5
Memory
Memory
Memory
Exercise PA1: Modify ptrArray06.c so that it only uses variables i and d[MAX].
Introduction
Exercise PA2: Modify ptrArray07.c so that the for loop contains only one
Pointers & Arrays
statement.
Intro
Array Addresses
Indexes & Pointers–I
Exercise PA3: Modify ptrArray08.c so that it sums the odd elements of a[M].
Indexes & Pointers–II
Indexes & Pointers–III
Exercise PA4: Modify ptrArray13.c so that it does not require the variable len and
Pointer types the for loop contains only one statement.
Pointer experiment
Using Pointers Exercise PA5: The search program ptrArray14.c reports only if an element exists
Array bounds in an array. Enhance this program so that it also prints out the index where the
Even elements
Copying
element was found and not found otherwise.
Reverse Copy
Flip
int to char
String Length
Search
Binary Search
Exercises PA1-PA5
Exercises PA6-PA8
Dynamic Memory
Exercise PA6: The following incomplete program reads in 3 strings and prints them
Introduction
out in dictionary order. Complete the nested if statement. No other changes can be
Pointers & Arrays
made. This program is about 40 lines long and may have only the one printf shown.
Intro
#include <stdio.h>
Array Addresses
#include <string.h>
Indexes & Pointers–I
Indexes & Pointers–II
#define MAX 3
Indexes & Pointers–III int main()
Pointer types {
Pointer experiment char a[MAX], b[MAX], c[MAX], *p1, *p2, *p3;
Using Pointers scanf("%s",a); scanf("%s",b); scanf("%s",c);
Array bounds if(...)
Even elements {...}
Copying printf("%s %s %s\n", p1, p2, p3);
Reverse Copy
}
Flip
int to char
String Length
Exercise PA7: Modify program ptrArray05.c so that it prints out the addresses
Search stored in the pointers pC and pI at each step. For pC this could be done as follows.
Binary Search
Exercises PA1-PA5
printf("%p %c\n", pc, *pc);
Exercises PA6-PA8 pc++;
Dynamic Memory Verify that the char pointer increases by one location at a time, while the int
pointer increases by a different amount (this will depend on your computer system).
Exercise PA8: Modify ptrArray09.c to print out the addresses contained in the
pointers at each step. Also print out the sizes of chars, ints and pointers using the
sizeof() function, e.g. printf(”%d”, sizeof(pa));. Are the addresses consistent with
the sizes? The answer to this question depends on the way your computer lays out
variables in memory.
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 36 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Introduction
Dynamic Memory
Intro-I
Intro-II
Example
In Action
malloc() & free()
Memory Leak
Allocating blocks
Malloc Failure
Run time allocation
Dynamic Memory
Swapping Pointers
Pointers to pointers
Exercises D1–D5
Exercises D6–D8
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
This causes the pointee to be left isolated in dynamic memory, with nothing pointing to it (as the original
pointer disappears when the scope closes).
Each time the loop executes a new pointee is left isolated dynamic memory.
If this loop were running for millions or billions of iterations, all dynamic memory would be exhausted.
a × b× p× q×
Stack Memory Dynamic Memory
malloc() can be used to allocate blocks of dynamic memory.
i ? a × b× p× q×
Stack Memory Dynamic Memory
malloc() can be used to allocate blocks of dynamic memory.
i ? a • b × p× q× ? ? ? ? ? ? ? ?
i ? a • b × p× q× ’a’’b’’r’’a’’c’’a’’d’’\0’
? ? ? ? ?
i ? a • b • p × q× ’a’’b’’r’’a’’c’’a’’d’’\0’
? ? ? ? ?
i ? a • b • p • q × ’a’’b’’r’’a’’c’’a’’d’’\0’
? ? ? ? ?
i ? a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
? ? ? ? ?
i 0 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’ ? ? ? ?
i 0 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’ ? ? ? ?
i 1 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’ ? ? ?
i 1 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’ ? ? ?
i 2 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’’r’ ? ?
i 2 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’’r’ ? ?
i 3 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’’r’’a’ ?
i 3 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’’r’’a’ ?
i 4 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’’r’’a’’\0’
i 4 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
’a’’b’’r’’a’’\0’
i 4 a • b • p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
i 4 a • b ×p • q • ’a’’b’’r’’a’’c’’a’’d’’\0’
i 4 a × b× p • q •
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
i ? c ? n ? a × p×
Stack Memory Dynamic Memory
We will first read in n and then allocate a block of n ints.
i ? c ? n 8 a × p×
Stack Memory Dynamic Memory
n is read in.
i ? c ? n 8 a • p × ? ? ? ? ? ? ? ?
i ? c ? n 8 a • p • ? ? ? ? ? ? ? ?
i ? c ? n 8 a • p • 3 -9 7 -2 4 7 -6 5
i ? c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 0 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 0 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 1 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 1 c 0 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 1 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 2 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 2 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 3 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 3 c 1 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 3 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 4 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 4 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 5 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 5 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 6 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 6 c 2 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 6 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 7 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 7 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 8 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 8 c 3 n 8 a • p • 3 -9 7 -2 4 7 -6 5
i 8 c 3 n 8 a • p × 3 -9 7 -2 4 7 -6 5
i 8 c 3 n 8 a × p×
Stack Memory Dynamic Memory
a is freed.
x × y× z×
Stack Memory Dynamic Memory
x • y × z× ? ? ? ? ?
x • y × z× ’t’’h’’i’’s’’\0’
? ? ? ? ?
x • y • z × ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z × ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z × ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z • ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z • ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z • ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z • ’t’’h’’i’’s’’\0’
’t’’h’’a’’t’’\0’
x • y • z × ’t’’h’’i’’s’’\0’
x • y × z× ’t’’h’’i’’s’’\0’
x × y× z×
Stack Memory Dynamic Memory
Introduction Exercise D1: Modify dynamic05.c so that it does not use temporary pointers p and
Pointers & Arrays q. The blocks should be accessed using array notation: b[i] = a[i].
Dynamic Memory
Intro-I Exercise D2: Modify dynamic05.c so that it does not use temporary pointers p and
Intro-II q. This time the blocks are accessed using the dereferencing notation *(b+i) etc.
Example
In Action Exercise D3: Modify the solution to Exercise D2, so that b copies over the last 4
malloc() & free()
Memory Leak
elements of a. The output of this program is: a=abracad b=acad
Allocating blocks
Malloc Failure Exercise D4: Explain what would happen in dynamic05.c if the statement:
Run time allocation *q = NULL; were replaced with q = NULL;
Swapping Pointers
Pointers to pointers Exercise D5: The following incomplete program reads in 10 integers into a and
Exercises D1–D5
then counts the number of negative values, c. It then mallocs an array n of size c
Exercises D6–D8
and fills it with only the negative values. Complete this program.
...
#define M 10
int main()
{
int a[M], *n, i, j=0, c=0;
getIntArray(a,M);
...
for(i=0; i<c; i++)
printf("%d ", n[i]);
...
}
c 2008 Algopath LLC. All Rights Reserved.
Copyright
STEP PAGE PREV – 53 / 54
C Programming:
Memory & Pointers
An Algopath eBook
Exercises D6–D8
Introduction Exercise D6: Write a program that allocates two character arrays a and b of length
Pointers & Arrays M and reads in two strings of length M-1 into these. It then allocates a third array
Dynamic Memory c and fills it with the elements of a and b in alternating order. For example if M=5
Intro-I and the input strings are WXPQ and abcd, c should be WaXbPcQd. Verify that
Intro-II
Example
your program works by printing out c.
In Action
malloc() & free() Exercise D7: Modify the program of D6 as follows. The input strings can now be
Memory Leak any length between 1 and M-1. The array should contain alternating characters
Allocating blocks from a and b as long as possible, and then the remaining characters from the longer
Malloc Failure
Run time allocation
string. For example, if the inputs are MX and acde, the output should be MaXcde.
Swapping Pointers The array c should not have any unused elements.
Pointers to pointers
Exercises D1–D5 Exercise D8: Write a program that uses pointees to create the structure shown on
Exercises D6–D8 the left. The characters ’a’, ’b’, ’c’ & ’d’ are read in from input. After creating the
structure, the program should print out the char values in the 4 locations. Pointers
should now be changed so as to create the situation on the right and the values
printed out again.