facebook like button

01 June, 2011

Concept of pointers in C

Most of you have heard the term pointer. Those who have not heard, don't worry. All of us are going to know some things about pointers in this post. Through my experience I can say that beginners think pointer to be tough part in C though its not tough but its one of the most important and the most powerful concepts. Let me first give you some examples why pointer was needed:

The need: 

1. Have a look at the program below which was written to exchange the values of 2 variables x and y:
--------------------------------------------
#include<stdio.h>
void swap(int a, int b);
main()
{
  int x=4,y=5;
  printf("values before swap are\n");
  printf("x=%d\ty=%d\n",x,y);
  swap(x,y);
  printf("values before swap are\n");
  printf("x=%d\ty=%d\n",x,y);
  return 0;
}
void swap(int a, int b)
{
 int tmp;
 tmp=a;
 a=b;
 b=tmp;
}
--------------------------------------------
Above program does not give desired output. Check it. The reason behind this is this. When we pass arguments to any function, only the values of argument variables (in our case x and y) are passed and assigned to new variables (in our case a and b) in called function. So when values of a and b are exchanged inside the function, values of x and y remains unchanged. Pointer can handle this problem which will be illustrated later.

2. In normal user defined functions multiple values(like arrays and structures cannot be returned.) For that we have to go to pointers.

Pointer: A pointer(or pointer variable) to any type of variable is a label that contains the address (starting byte address) of that variable in the memory. If a pointer variable p has the address of variable i, technically we say that the pointer p points to the variable x. Type a pointer is the type of the variable pointed. Pointer variables are declared by writing a * before the variable name.ex.
int *p;        //declaration of an integer pointer p


Points to remember about pointers:


Pointers are intended to have only address values so only number of operations on pointers are restricted. Some important points are as under:

1. For now think a pointer to be a normal variable which you will use only to hold addresses.

2. We cannot multiply,divide,add or subtract pointer variables. This is forbidden because the output of these may not be a valid address.

3. We cannot initialize (assign any value to) a pointer directly. This is because pointer can hold address of a valid location in memory. You'll later come to know that pointers can be initialised using dynamic memory allocation.

4. So pointers are initialized by giving address of a valid variable. sample code segment:
int x;         //declaration of an integer x
int *p;        //declaration of an integer pointer p
p=&x;         //initialization of an integer pointer p
after this p holds address of x.
printf("%d",p);
will print the value of address of variable x.

5. Now p can be used to access value of x. This means that the statement:
printf("%d",*p);
will print the value of variable x.

6. 2 pointers can point to same location. This is very obvious if you remember point 1 of this section (2 normal variables can have same values so 2 pointer variables can have same address values).

Additional advantages: 
 Use of pointers is not limited to written above. Everything that uses memory can be pointed by a pointer variable. Arrays, structures and even functions can have pointers pointing to their location because they also reside somewhere in memory. You people may have created text files using notepad or any other text editor. If we want to read or do any operation on text files, we can assign a pointer to point to that file. This is a complete branch of C language called FILE HANDLING in C. Different uses of pointers will be put as more programs are put in the posts.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply