CONCEPT OF POINTERS IN C PROGRAMMING


INTRODUCTION TO POINTERS

Pointers are extremely flexible and powerful tool for programming over a wide range of applications. The use of pointers offers a great degree of flexibility for manipulating data in programs and quicker and memory efficient. Most of the learners feel that understanding pointer is a very daunting task , but it is not so. The real power of pointers lies in situations such as

  • To just pass the address of data instead of passing whole copy of data to functions.
  • To return more than one value from the function.
  • Passing arrays and strings to functions more efficiently
  • Creation of linked data structures such as Linked Lists , Trees ,Graphs etcetera.

UNDERSTANDING POINTERS

In order to use pointers in an efficient way, the programmer must need to know how the information is stored in memory. The computer memory consists of millions of sequence of byte sized storage cells, with each cell having unique address to distinguish it from other cells in memory.

REPRESENTATION OF MEMORY

You have already learned how to declare a variable. Every time a variable is declared, it is given a name , data type is assigned and some memory location is reserved to store data for that variable. If a variable occupies more than 1 byte, the variable address is the address of the first byte.

For example:

int x = 9;

In the above statement, It allocates a block of 2 bytes of memory for storing integer constant 5 and associates a name x with it.

when the program uses a variable x , the compiler accesses its address so when the statement,

x = 20;

is executed, the compiler knows that the memory address of x is 2244 and sets its constant to 20.

 

ADDRESS OF OPERATOR

It is possible to get memory address of any variable used in a program by sing “address of” operator(&). It is also known as reference operator. The address of operator is a unary operator that return the memory address of an operand. In order to get the memory address of a variable. simply precede the name of the variable with address of operator.

The example below shows the use of address of operator:

EXAMPLE 1 : PROGRAM TO DEMOSTRATE USE OF ADDRESS OPERATOR(&)

#include<stdio.h>

#include<conio.h>

void main()

{

int x=9;

clrscr();

printf(“value is %d” is stored at address %u”,x,&x);

getch();

}

OUTPUT: value 9 is stored at address 656884

 

POINTER DECLARATION

A pointer variable is a variable that stores a memory address as its value. It can be used to indirectly access a value stored at a memory address.

A pointer variable is declared in the same way as that of normal variable except that the name of the pointer variable must be precede by asterisk(*). The syntax for declaring the pointer variable is :

datatype *ptrvariable ;

For example,

int *ptr;

where ptr is a pointer variable in which can store the address of any variable having data type int.

Note: we can use char,float and double data type also.

 

INITIALIZATION OF POINTER VARIABLE

When the pointer variable is declared, it is not initialized.

For example,

int *ptr;

In the above statement, pointer variable ptr will have some unknown value that will be interpreted as memory address. which may be an invalid address and gives inappropriate result. so it is important to initialize a pointer variable with a meaningful address before it is used in the program.

For example,

int a=10;

int *ptr;

ptr=&i;

where a pointer variable ptr which is initialized with the address of a integer type variable a.

In the second statement, pointer variable ptr is declared

In the third statement, pointer variable ptr is initialized with address of variable a .

 

 

Leave a comment