Prog II Chapter 1
Prog II Chapter 1
COMPUTER SCIENCES
Course Title: Computer 1
Programming II
Chapter 1:Pointers
05/11/2025
DEFINITION OF POINTERS
A pointer is a variable that stores the memory address of an object.
A variable which stores the address of another variable is called
a pointer.
Pointers are used extensively in both C and C++ for three main purposes:
To allocate new objects on the heap,
To pass functions to other functions
To iterate over elements in arrays or other data structures.
Variables are locations in the computer's memory which can be
accessed by their identifier (their name).
For a C++ program, the memory of a computer is like a
succession of memory cells, each one byte in size, and each with
a unique address..
By using these language and library facilities instead of raw pointers,
you will make your program safer, easier to debug, and simpler to
understand and maintain. 05/11/2025 2
MY FIRST PROGRAM
The numbers shows
line of source code Out put of the
program.
Sample
source
code
05/11/2025 6
BENEFITS OF USING POINTERS
Pointers save the memory.
Pointers reduce the length and complexity of a
program.
Pointers allow passing of arrays and strings to
functions more efficiently.
Pointers make possible to return more than one
value from the function.
Pointers increase the processing speed.
05/11/2025 7
DEFINITION OF POINTERS CON…
Here is an example of valid pointer declarations in C++:
int *x; // a pointer to integer
double *x; // a pointer to double
float *x; // a pointer to float
char *ch // a pointer to a character
05/11/2025 8
POINTERS - ADDRESS-OF OPERATOR (&)
05/11/2025 12
POINTERS AND STRING
There is a relationship between string and pointers.
In C++ string means character array.
When a character array is declared then only its first element address is
stored.
The rest of the elements can be accessed with the help pointer to
character array.
05/11/2025 13
POINTERS AND STRING CON…
05/11/2025 14
Pointer expression: We can add an integer or subtract an integer using a
POINTER OPERATORS, EXPRESSION, ARITHMETIC
05/11/2025 15
POINTERS AND ARRAYS
An array is a data structure which allows a collective name to be given to a group of
elements which all have the same type.
An individual element of an array is identified by its own unique index (or subscript).
An array declaration is very similar to a variable declaration.
The number of elements must be an integer.
The concept of arrays is related to that of pointers. In fact, arrays work
very much like pointers to their first elements, and, actually, an array
can always be implicitly converted to the pointer of the proper type.
For example, consider these two declarations:
int myarray [20];
int * mypointer;
The following assignment operation would be valid:
mypointer = myarray;
But the following assignment would not be valid:
myarray = mypointer; 05/11/2025 16
ARRAYS AND POINTERS
Array is a group of elements that share a common
name, and that are different from one another by their
positions within the array.
05/11/2025 17
ARRAYS AND POINTERS CON…
The name identifies the location in memory, big enough
to store the whole array.
a[k] refers to the k-th element of the array, the
indexing starting from 0.
The memory allocation happens when the array is
declared: use # to set the dimensions.
Advantages: clear and compact coding, better
modularity, take advantage of loops for repetitive
operations.
05/11/2025 18
ARRAYS
Out put
05/11/2025 19
END 05/11/2025 20