Pointers
▪ A Pointer is a variable which contains the address of
another variable.
▪ Declaration syntax:
Pointer_type *pointer_name;
▪ This declaration will create a pointer of the pointer_name
which will point to the type of variable of pointer_type.
Examples: int *p; float *x; char *w;
▪ There are two distinctions in this declaration:
▪ One is the obvious ‘indirection’ operator, i.e., the asterisk
‘*’ which must precede a pointer name.
▪ Another distinction is that the pointer_type actually
declares the type of the variable which is pointed to, by
the pointer, i.e., whose address the pointer contains.
1
Pointers
▪ The initialization of the pointer is done as follows:
▪ Initialization syntax:
pointer_name = &variable_name;
▪ Here ‘&’ is called the ‘Reference’ operator. When it
precedes some variable_name, it refers to the address of
that variable rather than the value.
▪ For example:
p = &a;
//would assign to variable ‘p’ the address of the variable ‘a’
2
Pointers
▪ The operators can be interpreted as follows:
▪ Indirection operator (dereference operator):
* = “The value of” ( the variable that is pointed to,
by the pointer)
▪ Reference operator:
& = “The address of” ( the variable)
▪ For example:
int a;
int *p;
p=&a;
*p = 10; //means the value of the variable ‘a’ that is
pointed to, by the pointer ‘p’.
// so ‘a’ will contain the value 10.
//this is called dereferencing the value of ‘a’ by ‘*p’.
3
Pointers
◼ In the preceding example, if we try to imagine what
happens as the program executes line by line (addresses
are assumed): a
• int a; value garbage A variable address is
reserved in memory
address [0318]
p
A pointer space is
• int *p; null reserved in
memory
[0569]
p a
• p=&a; The pointer ‘p’
0318 garbage
points to ‘a’
[0569] [0318]
p a
0318 ‘a’ is initialized by
• *p = 10; 10
dereferencing 4
[0569] [0318]
Pointers
▪ Pointer Type must be matched with the pointed variable
type. For example,
char *ch;
int *x;
float *y;
▪ The data type of ch is different from the type of x and the
type of y.
▪ Therefore, ‘ch’ must point to some char type variable, ‘x’
must point to some int type variable, and ‘y’ to float type.
▪ Run-time errors and compile-time warnings may occur in a
program that defines a pointer to one data type and then
uses it to point to some other data type
▪ Although pointers might point to different data type, they
themselves occupy the same amount of space in memory
(the size of a pointer depends on the operating system)
5
Pointers
▪ But the data to which they point do not occupy the same
amount of space nor are of the same type, one is int (4
byte) , another one is char (1 byte) and the other one
float (4 byte)
▪ The following code segment will exchange the contents
of the variables x and y by using the address and
dereferencing operators:
int x = 10, y = 20, temp;
int *ip;
ip = &x;
temp = *ip;
*ip = y;
y = temp;
6
Pointers
The first two statements reserves memory spaces for x,
y, temp, ip and also initialize x, y:
int x = 10, y = 20, temp;
int *ip;
▪ Let us assume that the cell named x is located at address
1395, that the cell named y is located at address 3321,
that the cell named temp is located at address 0579, and
that the address of the pointer variable ip is 1925 – which
can be shown graphically as follows:
x y temp ip
10 20 garbage null
[1395] [3321] [0579] [1925]
Allocation and initialization of memory cells
7
Pointers
The third statement initializes ip with the address of x:
ip = &x;
x y temp ip
10 20 garbage 1395
[1395] [3321] [0579] [1925]
Initialization of the pointer variable
temp = *ip;
x y temp ip
10 20 10 1395
[1395] [3321] [0579] [1925]
Assignment of ‘temp’ with dereferencing a pointer
8
Pointers
◼ The next statement assigns the value of y into the
variable pointed to ‘x’, by the pointer ‘ip’.
*ip = y;
ip
x y temp
10 1395
20 20
[0579] [1925]
[1395] [3321]
The value of ‘x’ is changed by dereferencing using a pointer ‘ip’
9
Pointers
▪ In the last statement, ‘temp’ value is assigned to ‘y’.
▪ y = temp;
x y temp ip
20 10 10 1395
[1395] [3321] [0579] [1925]
Memory locations after all the statements have been executed
▪ Thus, after all the statements in the code segment
have been executed, the values of x and y are
swapped.
10
Pointers
▪ We can define pointer variables that point to other
pointer variables which, in turn, point to the data, let’s
say a char – the relationship looks like the one shown
in the following figure.
variable pointer pointer
A 3219 1750
[3219] [1750] [0399]
A pointer to a pointer of type char
To define a pointer to a pointer, one simply increases
the number of asterisks preceding the identifier:
• char **cp;
11
Pointers
▪ The number of pointers that must be followed to access
the data item, or the number of asterisks that must be
attached to the variable to reference the value to which
it points, is called the level of indirection of the pointer
variable
▪ A pointer’s level of indirection determines how much
dereferencing must be done to access the data type given
in the definition
▪ The following example is an illustration:
int d = 5;
int *ip1;
int **ip2;
int ***ip3;
12
Pointers
◼ ip1 = &d;
◼ ip2 = &ip1;
◼ ip3 = &ip3;
d ip1 ip2 ip3
5 1112 2368 3219
[1112] [2368] [3219] [4498]
Three pointers using different levels of indirection
◼ In the above illustration, ip3 is assigned the address
of a pointer ip2 (not the contents of ip2) that
indirectly points to an integer variable d, through
another pointer ip1
13
Pointers
◼ C allows the programmer to perform two arithmetic
operations on a pointer address – addition and subtraction
using indirection operator ‘*’.
◼ The following program segment is an example
int *ip;
float *fp;
char *ch;
int x;
float y;
char w;
ip = &x;
fp = &y;
ch = &w;
ip++;
fp++; 14
ch++;
Pointers
▪ Assume that x is stored at memory cell address 2000, and y
is stored at memory cell address 4000, w in 6000.
▪ When the last three statements are executed, ip will contain
the address 2002 and fp will contain the address 4004 and
ch will contain the address 6001
▪ One can also modify a pointer’s address by using integer
addition and subtraction, for example,
ip = ip +2; fp = fp + 4; ch = ch +1;
▪ Arithmetic operations can be done by using ‘*’ operator, for
example,
*ip = *(ip + 1); *fp = *(fp + 1); *ch = *(ch +1);
[When one adds to or subtracts from a pointer using indirection,
the amount by which pointer shifts its value is determined by
the multiplication by the size of the variable type the pointer
points to, i.e., for integer multiply by 4, for float , multiply by 4
and for character, multiply by 1 ] 15
Pointers
▪ The address operator cannot be used on arithmetic
operations;
Not with expressions involving operators such as ‘+’ and ‘-’
▪ ip = &(v1 + 10); //wrong statement
▪ Benefits(use) of pointers in C:
▪ Pointers provide direct access to memory
▪ Pointers provide a way to return more than one value to
the functions. Usually Functions cannot return more
than one value. But functions can be written to modify
many pointer variables and then return more than one
value of the pointed variables.
▪ Reduce the storage space and complexity of the
program
16
Pointers
▪ Benefits(use) of pointers in C:
▪ Reduce the execution time of the program
▪ Provide an alternate way to access array elements
▪ Pointers can be used to pass information back and
forth between the calling function and called function.
▪ Pointers allow us to perform dynamic memory
allocation and de-allocation. In the case of arrays, we
can decide the size of the array at runtime by
allocating the necessary space.
▪ Pointers help us to build complex data structures like
linked list, stack, queues, trees, graphs etc.
▪ Pointers allow us to resize the dynamically allocated
memory block.
▪ Addresses of objects can be extracted using pointers 17
Pointers
▪ Drawbacks of pointers in C:
▪ Uninitialized pointers might cause segmentation fault.
▪ Dynamically allocated block needs to be freed
explicitly. Otherwise, it would lead to memory leak.
▪ Pointers are slower than normal variables.
▪ If pointers are updated with incorrect values, it might
lead to memory corruption.
18