Module7 - Pointers
Module7 - Pointers
com
Pointers in C++
Introduction
Pointers are extremely powerful because they allow accessing the addresses and
manipulating their data. If we utilizing pointers in correct manner then they could greatly
improves the efficiency and performance otherwise they could lead to many problems. The use
m
of pointers makes the program more efficient and compact. Some of the uses of pointers are:
co
• Improving efficiency
• Accessing dynamically allocated memory
n.
• Returning more than one value from a function
• Implementing data structures
io
Before going into pointers, it is important to understand how the memory is organized. In
at
computer the memory is made up of bytes (8 bits) arranged in a sequential manner, each byte is
uc
identified by an index number called the address of that byte. The address of the bytes starts from
zero and the address of the last byte is one less than the size of memory. For example 1KB
ed
memory has 1024 bytes and each byte given an address 0 to 1023. Already we have studied the
concept of variables in the previous modules a variable is a name for a piece of memory which
hi
holds a value. When we declare a variable in the program it automatically access the free
memory and assigned to the variable in random manner and any value assigned to that variable is
ks
The address operator allows us to see what memory address is assigned to a variable. This
w
Example:
w
int x;
Address of x = ‘&x’
www.sakshieducation.com
www.sakshieducation.com
The dereference operator allows us to get the value at a particular address. It is also called
indirection operator.
Finally after the introduction of memory, address operator and dereference operator it is time to
talk about pointers.
Pointers
m
A pointer is a variable that holds a memory address as its value. We perform some operations
co
with pointers very frequently.
• Define pointer variables.
n.
• Assign the address of a variable to a pointer using address operator ‘&’.
• Access the value at the address of variable using dereference operator ‘*’.
io
at
uc
ed
Declaring a pointer
ks
Pointer variables are declared like normal variables, with an asterisk in front of variable name.
Syntax:
a
data_type *pname;
.s
Initializing pointers
www.sakshieducation.com
www.sakshieducation.com
Pointers are variables so the compiler must reserve memory for pointers and they will
have some address. When declare a pointer variable, but not initialized then it contains the
random address of other memory locations, which is one of the most common errors encountered
when using pointers and it harder to debug the code. This situation is very dangerous which may
crash the program, so we must need to initialize a pointer variable whenever they declared.
Generally the local and global static pointers are automatically initialized to NULL but when we
m
declare an automatic pointer variable it contains the garbage value that is it may points to
anywhere in the memory locations. The use of an unassigned pointer may give unpredictable
co
results and it may crash the operating system also.
Example:
n.
int a=7;
io
int *ptr = &a;
Æ A pointer variable named ‘ptr’ that points to an integer variable named ‘a’.
at
int ‘*ptr’ declares the pointer to an integer value, which is initialized to address of ‘a’. Once a
uc
pointer is declared and initialized then we can deference it using dereference operator ‘*’ which
gives the actual value stored at that address.
ed
int *p = &a;
ks
www.sakshieducation.com
www.sakshieducation.com
m
co
Figure3: Output of the program
n.
Size of pointer
The size of pointer is depends upon the architecture. For 32-bit architectures uses 32-bits
io
i.e. 4 bytes and 64-bit architecture uses 64 bits i.e. 8 bytes. Pointers reserve fixed size of memory
at
irrespective of their base type, still we are mentioning data type because the value of pointer only
uc
tells the address of the starting byte but it does not know how many bytes of data will be
retrieved, so by using data type it knows that how many number of bytes should be retrieved
ed
from memory. The following example program shows the size of pointer variables and size of
values dereference by them.
hi
a ks
.s
w
w
w
www.sakshieducation.com
www.sakshieducation.com
m
Null pointers
co
A pointer holding a null value is called a null pointer i.e. initializes a pointer to ‘0’ or
NULL means points to nothing. While declaring pointers if unable to initialize with exact value
n.
then initialize as NULL pointers. NULL pointers are safer than the uninitialized pointers
io
because the computer stops the program immediately.
at
Example:
int *ptr = 0; or int *ptr=NULL;
uc
ed
hi
a ks
.s
w
w
www.sakshieducation.com
www.sakshieducation.com
m
Figure7: Output of the program
Pointer Arithmetic
co
The arithmetic operations are possible on pointers but not all arithmetic operations. The
n.
only valid operator’s that can be used on pointers are addition, subtraction, increment and
decrement. The pointer arithmetic is somewhat different from ordinary arithmetic operations.
io
Here all arithmetic operations are performed relative to the size of base type of pointer. To better
at
understanding pointer arithmetic let us consider an example integer pointer ‘ptr’ which points to
address 8000 and perform increment operation on ‘ptr’ is ‘ptr++’. Now the ‘ptr’ will point to the
uc
location 8004 instead of 8001. This is because the size of int is four bytes. This operation will
move the pointer to next memory location without changing actual value at the memory location.
ed
If ‘ptr’ is a character pointer whose address is 2000 and perform increment operation on this will
point to memory location 2001 because the size of char is one byte.
hi
ks
Pointer comparisons
The pointers can be compared with relational operators such as ‘= =’, ‘! =’, ‘>’, ‘>=’, ‘<’,
a
‘<=’. The operators ‘= =’, ‘! =’ are used to compare two pointers for finding whether they
.s
contain same address or not and they are equal if both pointers contain NULL or they points to
w
address of same variable. The operators ‘>’, ‘>=’, ‘<’, ‘<=’ are used for only same type pointers.
w
These operations are making sense only when both pointers point to elements of same array.
w
Pointer to pointer
A pointer to a pointer is a form of multiple indirection of pointer that is a pointer points to
another pointer which points to the variable. As shown in below figure the first pointer contains
the address of second pointer which contains the address of variable which contains the value.
The pointer to pointer concept can extended to any limit but most of the time pointer to pointer is
www.sakshieducation.com
www.sakshieducation.com
used. The pointer to pointer variable is declared as an additional asterisk comes in front of
pointer variable as shown in below example,
Example:
int **ptr;
m
co
n.
io
Figure8: Pointer to Pointer
at
uc
ed
hi
a ks
.s
w
w
w
www.sakshieducation.com
www.sakshieducation.com
m
co
Figure10: Output of the program
Void Pointer
n.
The void pointer is also called as generic pointer. A void pointer can hold the address of
io
any data type. Void pointer is a special type of pointer which can be pointed to any data type.
at
The void pointer can be assigned to a pointer of any data type and address of any data type
without any explicit casts. The void pointer is declared like a normal pointer using the ‘void’
uc
keyword as pointers type. We cannot operate on the object pointed by void pointer because the
type is unknown. The void pointers can be used compare with another address.
ed
Syntax:
void *ptr; Æ ptr is a void pointer.
hi
Examples:
ks
int i = 7;
float f = 7.7;
void *ptr;
a
ptr = &i;
.s
ptr = &f;
w
A void pointer can’t be dereferences by using indirection operator, before dereferencing it should
w
be type cast to appropriate pointer data type. The pointer arithmetic operations are not performed
on void pointers without typecasting. The void pointers are mostly used to pass pointer to
w
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
Figure11: Void pointer example
uc
ed
hi
a ks
A non constant pointer to a const value is a pointer that points to a constant value. To
declare a pointer to const value, use const keyword before the data type. The value pointed to
w
www.sakshieducation.com
www.sakshieducation.com
Const pointers to non const value
A const pointer is a pointer whose value cannot be changed after initialization. The const
pointer can be declared using the const keyword between the asterisk and pointer name. The
value pointed to can be changed but pointer cannot be changed to point another value.
int a = 7;
int *const ptr = &a;
m
Const pointer to a const value
co
A const pointer to const value is declared by using const keyword both before the type and
variable name. This pointer cannot be set to point another address nor can the value it is pointing
n.
to be changed through the pointer. The value pointed to cannot be changed and pointer cannot be
changed to point to another value.
io
int a = 7;
at
const int *const ptr = &a; uc
Non const pointer to non const value
The value pointed to can be changed and the pointer can be changed to point to another
ed
value.
int a = 7;
hi
Const pointers are used in function parameters to ensure that the function does not accidently
change the passed in the argument. A non const pointer can be redirected to point to other
a
addresses. A const pointer always points to the same address and this address cannot be changed.
.s
A pointer to non const value can change the value it is pointing to.
w
w
w
www.sakshieducation.com