Social Icons

Wednesday 19 February 2014

Pointers in C

In this lesson we discuss about pointers in c language.We have known about control structures,arrays,arithmetic expressions in previous sessions.
DEFINITION OF POINTERS:  
  1. Pointer,as the name indicates that it points to something that something is that we discuss now.Pointer is nothing but a variable which has the address of another variable.
  2. Pointers are largely used in C language and they make our program very simple and easily understandable
  3.  Pointers and arrays are very closely linked to each other and in pointers we mostly use goto statement
POINTERS AND ADDRESS:
  1. Let us assume that c is a character type and p is a pointer which points to c this situation we can implement in the program by an operator & which tells the address of a particular object so in our example we can write as (p=&c;).Which tells that address of c will be assigned to p.
  2. Rather than operator & we can also use * for the pointers let us see how these two are implemented from following program
                      int x = 0, y = 1, z[5];
                      int *ip;          /* ip is a pointer to int */
                      ip = &x;       /* ip now points to x */
                      y = *ip;          /* y is now 0 */   
                      *ip = 1;          /* x is now 1 */   
                      ip = &z[0];       /* ip now points to z[0] */ 
now have you understood what is the difference between the two if not read below points 
  • int *ip says that *ip is an integer type and ip=&x says that ip assigns the value of x        which is 0
  • Then y=*ip means that the value of ip will be pointed that is 0 so value of y will become 0
  • *ip=1 means  the value of *ip is x and its value becomes as 1 and at last ip points to z[0] 
POINTERS AND ARRAYS: 
The relationship between pointers and arrays are very strong in c language.This is explained in the following
  1. In an array we have declared a[10] means which has a blocks of size upto 10 and if we use pointers to the arrays we can point a particular array of size 10                           example:if we have declared as int a[10] means it provides size 10 and let us consider p is a pointer first position in the array we declare it as                                                                    int *p;                                                                                                                                p=a[0];
  2. In the above example p is a pointer which points to array location 0  which means it contains the address of a[0];

No comments:

Post a Comment