Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Pointers

Instructor: Noor Ullah Khan


What is Pointer?
Uses

• Accessing Array Elements


• Passing arguments to a function
when the function needs to
modify arguments.
• Passing arrays and strings to
function.
• Obtaining memory from the
system.
• Creating data structures such as
Linked List.
But

Function can
Arrays can be
modify
modified
variables
without
without using
pointers.
pointers
Then why use pointers?

TO CREATE LINKED LIST OR BINARY VIRTUAL FUNCTIONS, NEW/DELETE


TREES. OPERATOR AND THIS POINTER REQUIRES
POINTER.
Each byte in the computer
memory has an address.

Address and
Pointers
• Address are numbers just as there are for
homes on the street.
• Starts from 0 and go on 1,2,3…….
• If you have 1MB of memory the highest
address is 1,048,575.
What is
• Whenever your program is loaded in
address? memory . It occupies a certain range of
these addresses.
• Every function/variable starts at a particular
address.
• How to find address of a variable?
• Using address of operator(&)
The address • Remember address is not same as contents
of variable.
of • << insertion operator interprets the address
Operator(&) in hexadecimal as indicated by (0X).
• Every integer variable occupies 2 bytes
exactly for (16-bit system).
• We can print out values of addresses but it’s
meaningless?
• We want variables to store these addresses.
• A variable that holds an address value is
called a pointer variable, or simply a pointer.
Pointer • What is datatype of pointer?
variable • It’s the same as the variable of whose address is
being.
• Can’t we use general pointers?
• No, compiler needs to know what, kind of
variable the pointer points to.
Accessing the variable pointed to:

To access value at an address. Asterisk used while declaring


pointer means pointer to.
When asterisk(*) is used before a pointer
variable name it is called dereference operator.
In case you didn’t initialize pointer Compiler won’t give you a warning. Moral:
variable, it could contain garbage
addresses(Rogue pointer).
Make sure you give every pointer variable a valid
address before using it.
Pointers and
Arrays
• Name of an array is it’s address.
• +1 to this address means to move
to next element in the array.
• Compiler knows to add 2 bytes(in
case of 16-bit system) to address.
• How?
• Because you declared array
as int.
Pointer constants and
pointer variables
• Can you do *(arr++) with array name?
• No, you can’t increment a constant.
• Arr is the address where your system placed
your array.
• You can’t say arr++ like 7++.
• While you can’t increment an address, you can
increment a pointer that holds an address.
Pointers and We have seen that if
function needs to We pass it by reference.

Functions modify a variable.

Void
Either as reference or as
centimize(double&) or
pointer.
centimize(double*)

A reference is alias for


Both are same but the original variable,
mechanism is different. while a pointer the
address of the variable.
• Since the name of an array is the
array address, there is no need for
the address(&) operator when the
function is called.
Passing
Arrays
• C-type strings are simply arrays of
type char.
• Char str1[] = “defined as an array”
Pointers and • Char* str2 = “defined as a pointer”
C-Type • Str1++ // can’t do it.
Strings • Str2++ // can do it.

You might also like