Introduction to pointer in C | pointer operations in C

Preview

Introduction to pointer in C

Pointer is way to refer to a memory location. A pointer variable is a variable which can contain the address of another data item which may be a variable or an array. A variable has three basic properties including its name,  value and  memory location.

Suppose we declare an integer variable x having value 14 as follows:

int x=14;

Variable x has three properties as follows:

  • Name of Variable : x
  • Value of Variable : 14
  • Memory Location: 65526

1. Name of variable:

Programmer defines the name of a variable. It should follow all the rules of a valid identifier of C language. In the above example, x is a variable of int type. It also specifies that variable x consume two bytes of memory as int is of 2 bytes.

2. Value of variable:

It is the value assigned to the variable. It can change during the program execution. In the above example variable x has been assigned value 14.

3. Memory location of variable:

Variable is always stored at some location in main memory. It is always a numeric address which can be viewed as an unsigned integer number which may be in decimal form or in hexadecimal form.

In the above example, address of variable x is 65526. This address is machine dependent.

We can declare pointer variable of any valid data type of C language but there is a restriction that the variable whose address is stored in pointer variable should have same data type as of pointer variable.

It should be noted that a pointer variable doesn’t represent the value of a variable.

Operators associated with a pointer variable

There are two operators associated with pointers. They are as follows:

i. Indirection Operator (*)

This operator is also known as value operator or pointer operator. This operator is a unary operator which is used during variable declaration to specify that variable being declared is a pointer variable.

This operator is also be used to refer to the value of variable whose address is contained in the pointer variable.

Example:

int *x;

In above example variable x is declared as a pointer variable as it starts with an asterisk sign (*).

ii.Address Operator (&)

This operator is used to store address of a variable in a pointer variable. It can also be used to refer to the memory location of a variable.

Example:

int x=10,*y;
y=&x;

In above example, address of variable x has been stored in pointer variable y.

 



NULL Pointer

The constant NULL is a special pointer value which specifies “points to nothing.” It is a well defined pointer value which represents that a pointer variable does not have any address stored in it.

It is a run-time error to de-reference a NULL pointer. NULL is equal to the integer constant 0.

Accessing address of a variable

The actual memory location of a variable in memory is system dependent and therefore the address of a variable is not known to us beforehand.

We can determine the address of a variable with the help of address operator (&).

Example:                         

int x=14,*y;
y=&x;                                  




Accessing value of a pointer variable

We can access value of a pointer variable using indirection operator.

Example:        

int x=14,*y,z;
y=&x;        /* y contains the address of variable x*/
z=*y;          /* *y contains the value of variable x whose address is contained in pointer variable y*/

In this example, x and z are int type variables, x contain 14. y is an int type pointer variable which contains the address variable x. z is an int type variable which contains value of *y i.e. 14 which is the value of variable x whose address is contained in pointer variable y.




Initialization of a pointer variable

It is the process of storing the address of a variable in pointer variable while declaring pointer variable.

Example         

int x=14;
int *y=&x;

In this example, x is an int type variable that contains value 14. y is a pointer variable which contains the address of variable x. Address of variable x has been assigned to pointer variable y during its declaration. So it is known as initialization of pointer variable.

A pointer variable should not be used before it is initialized. It should also be ensured that a pointer variable always points to variable of same data type i.e. the data type of pointer variable and the variable whose address is stored in the pointer variable should be of same data type.




Advantages of a pointer

  • Pointer increases the execution speed of program as memory locations are directly manipulated.
  • It can be used to pass information back and forth in a function.
  • With the help of pointers we can save storage space.
  • Pointers reduce the length and complexity of a program.
  • Pointers help to pass a function as an argument to another function.
  • A pointer variable can be used to return more than one value to the calling function which is not possible in simple function as a function can return only one value at a time using return statement.
  • Pointers allow us to dynamically request memory to store information for use later.
  • Pointers allow us to create linked lists and other algorithmically oriented data structures.
  • Pointers have a close relation with arrays and strings. We can access individual elements of such data structures with the help of pointer variables in a better way.
  • Pointers help us to use variables outside the function. Mostly, the variables declared within a function can be used inside the same function but by using pointers we can use such variables outside the function.
  • Using pointers we can develop our own library of functions.
  • There are few applications which can only be presented in better way with the help of pointers.
  • Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.
  • In the case of arrays, we can decide the size of an array at runtime by allocating the necessary space.




Limitations of Pointer

  • During runtime, if sufficient memory is not available for the storage of pointers, the program may crash.
  • If the programmer is not careful with the use of pointers, the program may crash.
Program to demonstrate the use of pointers
#include<stdio.h>
int main()
{
int a,*b);
a=15;
b=&a;  /*& is known as address operator*/
printf(“\na=%d”,a);   /*a=15*/
printf(“\n&a=%u”,&a); /*&a=65524*/
printf(“\nb=%u”,b);
printf(“\n*b=%d”,*b);
return (0);
}
Output
a=15
&a=65524
b=65524
*b=15

Pointer Expressions

Just as normal variables, we can also use pointer variables in expressions. All the calculations and manipulations can be performed on pointer variables just as we perform on normal variables.

Example

int x,*y;
x=14;
y=&x;
*y=*y*2;       =>        *y= 14*2         =>       *y=40

Program to demonstrate the use of pointer expressions
#include<stdio.h>
int main()
{
int a,*b;a=15;
b=&a;
*b=*b*2;
printf(“\n*b=%d”,*b);   /*b=30*/
printf(“\na=%d”,a);  /*a=30 */

++*b;   /*Value referenced by b gets incremented*/ 

printf(“\nb=%u”,b);     /*b=65524 */
printf(“\n*b=%d”,*b);  /**b=31 */
printf(“\na=%d”,a);   /*a=31 */
*b++;    /*address stored in b gets incremented*/

 

printf(“\nb=%u”,b);   /*b=65526 as b is of int type*/
printf(“\n*b=%d”,*b);  /*b=0 */
printf(“\na=%d”,a);   /*a=31 */
return(0);
}

Output
*b=30
a=30
b=65524
*b=31
a=31
b=65526
*b=0
a=31

 


Pointer Arithmetic

We can increment or decrement pointer variables just as we do with normal variables but when we increment or decrement a pointer variable, address stored in it is incremented or decremented.

Rules for Pointer increment/Decrement:

  • Only addition, subtraction, increment and decrement operators can be used for pointer increment or decrement.
  • Multiplication or Division operators can’t be used for incrementing or decrementing a pointer variable as they may result in memory overflow or memory underflow.

Overflow means that the pointer may go across largest memory location due to multiplication and underflow means the pointer may contain address lesser than the smallest memory location due to division operation.

Moreover, the division operation may give answer in fractional form which is wrong as the memory address is always represented as an integer number.

Example:

int x,*y;
x=15;
y=&x;

In this example, pointer variable y contains the address of another variable x.

Let us suppose that y contains 65524 as the address of x.
If we write the statement y++
Address stored in y will be incremented by 2 bytes because int data type contains 2 bytes. So after increment, address contained in y would become 65526.

Similarly if y is a pointer variable of float type, then after increment the address would be 65528 because float data type contains 4 bytes.

Program to demonstrate Pointer increment/Decrement
#include<stdio.h>
int main()
{
int a,*b;
clrscr();
a=15;
b=&a;printf(“\nb=%u”,b);
printf(“\n*b=%d”,*b);++b; /*Address would be incremented by 2 bytes*/printf(“\nb=%u”,b);
printf(“\n*b=%d”,*b);

b+=2; /*Address would be incremented by 4 bytes*/

printf(“\nb=%u”,b);
printf(“\n*b=%d”,*b);

– -b;

printf(“\nb=%u”,b);
printf(“\n*b=%d”,*b);

return(0);
}

Output
b=65524
*b=15
b=65526
*b=0
b=65530
*b=0
b=65528
*b=344

Best Books of C





Lesson tags: address of a variable in c, advantages of pointer in c, arithmetic operations on pointer in c, initialization of pointer variablein c, introduction to pointer in c, pointer expressions in c, pointer variable in c
Back to: C Programming Language