You are on page 1of 7

Lecture 01

Pointers and Arrays


Pointers and Arrays

1.1 Pointer
A pointer is a variable that stores a memory address. Pointers are used to store the addresses of
other variables or memory items. A pointer with the value 0 or NULL points to nothing and is
known as a null pointer.

1.2 Pointer operators


There are two pointer operators.
(i) Address of Operator (&)
(ii) Indirection Operator (*)
1.2.1 Address of Operator (&)
The address operator (&) is a unary operator that obtains the memory address of its operand.

int y = 5, *yPtr;

yPtr = &y;

here *yPtr is a pointer variable that stores the memory address of y. here & sign is used to denote
the memory location of y.

1.2.2 Indirection Operator (*)


The * operator, commonly referred to as the indirection operator or dereferencing operator.
This works as follows.
cout << *yPtr << endl;

the above statement shows the value of “y” as below line.

cout << y << endl;

Using * in this manner is called dereferencing a pointer. The dereferenced pointer may also be
used to receive an input value as in it.

cin >> *yPtr;


1
1.3 Passing values to Pointers
There are two ways to pass values to pointers.
(iii) Pass by value
(iv) Pass by reference
1.3.1 Pass by value
By passing value means that we are passing the copy of actual parameters to the function. It
takes more memory because new variables are made to store the same data.

Figure 1.1 pass by value

2
1.3.2 Pass by reference
By passing value means that we are passing the address actual parameters to the function. Any
changes made to the passed values will be applied to the actual variables. It is memory efficient
way of passing value at big level.

Figure 1.2 pass by reference

1.4 Pointers and arrays


An array is represented by a variable that is associated with the address of its first storage
location. A pointer is also the address of a storage location with a defined type, so compiler
permits the use of the array [ ] index notation with both pointer variables and array variables.

3
Figure 1.3 pointers with arrays

4
1.5 Arithmetic operations at Pointers

1.5.1 Increment operator

Figure 1.4 Use of increment operator

5
1.5.2 Decrement operator

Figure 1.6 Use of Decrement operator

You might also like