0% found this document useful (0 votes)
61 views20 pages

Prog II Chapter 1

The document provides an overview of pointers in C++, defining them as variables that store memory addresses of other variables and explaining their uses, such as memory allocation and function passing. It includes examples of pointer declarations, the use of address-of and dereference operators, and the relationship between pointers and arrays. Additionally, it highlights the benefits of using pointers, including memory efficiency and increased processing speed.

Uploaded by

estifanos haile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views20 pages

Prog II Chapter 1

The document provides an overview of pointers in C++, defining them as variables that store memory addresses of other variables and explaining their uses, such as memory allocation and function passing. It includes examples of pointer declarations, the use of address-of and dereference operators, and the relationship between pointers and arrays. Additionally, it highlights the benefits of using pointers, including memory efficiency and increased processing speed.

Uploaded by

estifanos haile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

DEPARTMENT OF

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

 // my first program in C++ This is a comment line.


 All lines beginning with two slash signs (//) are considered comments and do not have
any effect on the behavior of the program.
 #include Lines beginning with a hash sign (#) are directives for the preprocessor.
 They are not regular code lines with expressions but indications for the compiler's
preprocessor.
 This specific file (iostream) includes the declarations of the basic standard input-output
library in C++, and it is included because its functionality is going to be used later in 3
05/11/2025
the program.
CON…
 using namespace std; All the elements of the standard C++ library are declared within
what is called a namespace, the namespace with the name std.
 This line is very frequent in C++ programs that use the standard library, and in fact it will be
included in most of the source codes included in these tutorials.
 int main ()
 This line corresponds to the beginning of the definition of the main function.
 The main function is the point by where all C++ programs start their execution, independently of its
location within the source code.
 The word main is followed in the code by a pair of parentheses (()).
 Right after these parentheses we can find the body of the main function enclosed in braces ({}).
 What is contained within these braces is what the function does when it is executed.

 cout << "Hello World!";


 This line is a C++ statement.
 In fact, this statement performs the only action that generates a visible effect in our first program.
 cout represents the standard output stream in C++, and the meaning of the entire statement is to
insert a sequence of characters
 return 0;
 The return statement causes the main function to finish.
 return may be followed by a return code (in our example is followed by the return code 0).
expected 4
05/11/2025
 A return code of 0 for the main function is generally interpreted as the program worked as
without any errors during its execution.
INITIALIZATION OF
VARIABLES
type identifier = initial_value ;
 For example, if we want to declare an int variable
called a initialized with a value of 0 at the moment
in which it is declared, we could write:
Int a=0;
 The other way to initialize variables, known as
constructor initialization, is done by enclosing the
initial value between parentheses (()):
Int a(0);
05/11/2025 5
CHARACTER AND STRING LITERALS

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 (&)

 The address of a variable can be obtained by preceding the name of


a variable with an ampersand sign (&), known as address-of
operator.
 For example:
foo= &myvar;
 This would assign the address of variable myvar to address; by
preceding the name of the variable myvar with the address-of
operator (&), we are no longer assigning the content of the variable
itself to foo, but its address.
 Let's assume, in order to help clarify some concepts, that myvar is
placed during runtime in the memory address 1776.
 In this case, consider the following code fragment:
 myvar = 25;
 foo = &myvar;
05/11/2025 9
 bar = myvar;
POINTERS - DEREFERENCE OPERATOR (*)

 As just seen, a variable which stores the address of another variable is


called a pointer.
 Pointers are said to "point to" the variable whose address they store.
 An interesting property of pointers is that they can be used to access the
variable they point to directly.
 This is done by preceding the pointer name with the dereference operator
(*).
 The operator itself can be read as "value pointed to by".
 Therefore, following with the values of the previous example, the following
statement:
Abc = *foo;
 This could be read as: “Abc equal to value pointed to by foo", and the
statement would actually assign the value 25 to Abc, since foo is 1776,
and the value pointed to by 1776 (following the example above) would be
05/11/2025 10
25.
DECLARE POINTERS
 Due to the ability of a pointer to directly refer to the value
that it points to, a pointer has different properties when it
points to a char than when it points to an int or a float.
 The declaration of pointers follows this syntax:
 dataType
type * name;
*identifier;
 where type is the data type pointed to by the pointer. This
type is not the type of the pointer itself, but the type of
the data the pointer points to.
 For example:
 int * number;
 char * character;
 double * decimals;
05/11/2025 11
#SAMPLE DEMO POINTER OPERATOR
Out put

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

pointer pointing to that integer variable.


 You can perform arithmetic operations on pointers.
 These operations are:
Increment and decrement
Addition and subtraction
Comparison
Arrays
 The increment (++) operator increases the value of a pointer by the size of the
data object the pointer refers to.
 The decrement (--) operator decreases the value of a pointer by the size of the data
object the pointer refers to.
 You can compare two pointers with the following operators: ==, !
=, <, >, <=, and >=.

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

You might also like