What are pointers? C

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’.

What are pointers?

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.

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 (*)

pointer variable declaration

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

address of integer variable
Collecting the statement together

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 */

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


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics