List Of Topics

Pointers

Until now we have studied variables, array and functions. In this chapter we will study pointers which are nothing but variables storing addresses. Address of other variables, array and also functions. In chapter on functions and strings, we have taken a slight introduction on pointers and seen how values can be passed by reference and more than one value can be returned from functions using pointers.

In this chapter we will study pointers in detail like relationship between arrays and pointers, what operations can be performed on pointer & how pointers are operated. We will also introduce the concept of pointers to function.

Though pointers seem a bit complicated at the outset there is nothing as interesting and useful as pointers.

What are pointers?

In order to understand what we mean by pointers, let us first understand how variables are stored in memory. When we define a variable; memory space depending upon the data-type of the variable is assigned to the variable.

E.g.
If variable is integer then 2 bytes are assigned to the variable.
If variable is character then 1 byte is assigned.
When we defined int i = 3; two bytes are allocated to variable ‘i’.

We can consider the analogy of variable being a house whose name is ’i’ . The value 3 stays in it having
house number as 2001. This 2001 is called the address of a variable.

Any variable stored in memory has a memory address.

A pointer is a variable itself, which stores the address of another variable of some specific type. So what is the difference between a pointer and other variables? The contents of pointer are address of another int, float, char etc. whereas contents of other variable are an int, float, char etc. Address of a variable is given by ampersand notation (&), which is called the unary operator or address operator that evaluates address of its operand.

Hence address of int i is &i. In pointer variable declaration, variable name must be preceded by an
asterisk (*)

We define a pointer variable as int *j where j is a pointer variable which contains the address of an integer variable. The ‘*’ notation is used with pointer variables to give the value of the variable pointed to by a pointer variable.

j = &i; says that j contains the address of integer variable i
i.e. j = 2001

Collecting the statement together

`int i = 3;int *j;j = &I;`

i is an integer variable containing 3.
j points to i.
j gives the contents of the address it contains.

Example(1)

/* Program demonstrating the concept of pointers
The use of * and & operators is shown */

`#include<stdio.h>main ( ){int i = 3;int *j;j = &i;printf ("n%d %u %u %dn", i, &i, j, *j);}`

Above program prints the values of i, &i, j and *j respectively. A point to be noted and remembered is that a pointer points to a particular kind of object i.e. every pointer point to a specific data type.

If j point to integer i, then *j can occur anywhere where i could.
If we say

```i = i + 1; i++; ++i.
j = j +1; (*j)++; ++ (*j)```

 e-University Search   Related Jobs No Jobs Found.