100% found this document useful (1 vote)
98 views

Programming Fundamentals: Pointers

The document discusses pointers in C++. It defines pointers as variables that store memory addresses and notes they are one of the most powerful tools for C++ programmers. It explains that variables are stored at unique memory locations, and pointers can store these memory addresses. The document outlines key pointer concepts like address-of operator, dereference operator, initializing pointers, distinguishing pointers, addresses, and variable values.

Uploaded by

kanwal hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
98 views

Programming Fundamentals: Pointers

The document discusses pointers in C++. It defines pointers as variables that store memory addresses and notes they are one of the most powerful tools for C++ programmers. It explains that variables are stored at unique memory locations, and pointers can store these memory addresses. The document outlines key pointer concepts like address-of operator, dereference operator, initializing pointers, distinguishing pointers, addresses, and variable values.

Uploaded by

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

Programming Fundamentals

Pointers
Pointers
• One of the most powerful tools available

to a C++ programmer is the ability to

manipulate computer memory directly

by using pointers

Programming Fundamentals- Pointers 2


What Is a Pointer?
• A pointer is a variable that stores a memory
address
• Computer memory is divided into sequentially
numbered memory locations
• Each variable is located at a unique location
in memory, known as its address
• The address stored in a pointer usually
corresponds to the position in memory where
a variable is located

Programming Fundamentals- Pointers 3


What Is a Pointer?

int num = 10;

num

10
709 710 711 712 713

Programming Fundamentals- Pointers 4


Address-Of (&) Operator
• Before looking into Pointers, lets look at
“address-of” operator (&)
• If we use address-of operator before a
variable name it returns the memory address
of that variable
int num = 10;
cout<<"Address:"<<&num<<endl;

Programming Fundamentals- Pointers 5


Storing the Address in a
Pointer
• Every variable has an address
• We can store that address in a pointer
• For declaring a pointer we use the character asterisk
(*) before a variable name with the type of the
address location which will be saved in it
• For example: if pNum is a pointer to an integer in
memory then
int *pNum;

Programming Fundamentals- Pointers 6


Initializing a Pointer
• A pointer whose value is zero is called a null pointer
int *pNum = 0;
• All pointers when they are created, should be
initialized to something. If you don't know what you
want to assign to the pointer, assign 0
• A pointer that is not initialized is called a “wild
pointer”
• Wild pointers are very dangerous

Programming Fundamentals- Pointers 7


int num = 10;
num

10
709 710 711 712 713

int *pNum = &num;


pNum

711
The Indirection
Operator
• The indirection operator (*) is also called the
dereference operator

• When a pointer is dereferenced, the value at the


address stored by the pointer is retrieved
cout<<"Value where pointing:” <<*pNum<<endl;

*pNum = 20;

Programming Fundamentals- Pointers 9


The Indirection
Operator
• From this example we can see that the
indirection Operator means

“the value stored at”

• Keep in mind that symbol * is used in two


distinct ways with pointers: declaration and
dereference

Programming Fundamentals- Pointers 10


The Indirection
Operator
• When a pointer is declared, the asterisk (*)
indicates that it is a pointer, not a normal variable
int *pNum;

• When the pointer is dereferenced, the indirection


operator indicates the value at the memory
location stored in the pointer
cout<<*pNum;

Programming Fundamentals- Pointers 11


Pointers, Addresses, and
Variables

• It is important to distinguish between a


pointer, the address that the pointer holds,
and the value at the address held by the
pointer
• This is the source of much of the confusion
about pointers
• Consider the next example

Programming Fundamentals- Pointers 12


Pointers, Addresses, and
Variables
int theVariable = 5;
int * pPointer = &theVariable;

• TheVariable is declared to be an integer variable


initialized with the value 5
• pPointer is declared to be a pointer to an integer; it is
initialized with the address of theVariable
• The address that pPointer holds is the address of
theVariable
• The value at the address that pPointer holds is 5
Programming Fundamentals- Pointers 13

You might also like