Saturday, July 27, 2013

     POINTERS IN C

             you have got in to the right place to know about pointers in c language,pointers have been the highlighting feautre in c language,but
as it is considered little hard to work out, it has been a nightmare to the begineers,before getting into concept of pointers ,let's have a description about them

              we are going to assume some real time examples in order to understand easily,and i swear that you ll be versed in pointers when you are leaving this page,okay let see the general definition of pointers:
  

pointers are the special variables which contains the address of another variables

in above example we can determine that the pointers are able to contain the address of another variable,let assume a real time example:if you meet your friend after a long gap of time and you invite him to your home ,how would you do it?
by giving your address to your friend ,you can make him to find your address,
                yes,like that every variable has certain unique address,if we are 
able to access the address of variables ,we ll be able to obtain their values 
too, to get know their addresses ,pointers are heavily employed
                 for instance:
                                       int s;        /*normal variable declaration*/

                                       int *s;     /*pointer variable declaration*/

now the pointer variable(int *s)is able to contain the address of another variable ;
Take a look at following snippet:
                      
                                                   int a;       /*normal variable declaration*/      

                                                   int *s;     /*pointer variable declaration*/

                                                   s=&a;      //pointer variable s points to ordinary variable a//

                                                   yes now we are able to find the address of a variable if we print the pointer :to wrap up to a complete snippet ,take a look at following code:
                                       #include<stdio.h>
                                       int main()
                                        {
                                           int a=10;
                                           int *s;
                                           s=&a;
                                           printf("the address of A variable is %x",s);
                                           printf("the value of A variable is %d",a);
                                           printf("the address of A variable is %x",&a);
                                           printf("the value of A variable is %d",*s);
                                          return 0;
                                        }
                           In the first line of program ,a is the variable filled with the value of 10,and s is the pointer variable was declared in the second line,
third line:pointer variable s points to the a variable ,therefore the address 
of a variable is stored into the s 
                           the first printf statement tells you that the address of variable a,it was achieved with the help of (s) and (%x) is used to display the address of a variable,note that if we specify the * in printf statement along with the pointer variable ,the value of variable is pointed will be displayed instead of displaying the address
                            we can do more experiments with the pointer ,for example ,we can make pointers as substitute of array while displaying the contents of array ,in the following section you are going to learn about this and pointer to array concept
                         int x[];              /*array variable declaration*/
       
                         int *z;
                         z=x;                 /*pointer variable z points to x*/

Above snippet are the procedures to be followed whenever you are going to make your pointer points to an array
To get a full snippet,observe carefully the following section:

                      #include<stdio.h>
                      int main()
                        {
                                 int x[10],i;
                                 int *z;
                                 z=x;
                                 for(i=0;i<10;i++)
                                   
                                        printf("the content of x[i] is  %d",*(z+i));

                                  return 0;
                          }

note that when you are to point to an array ,no (&) should be used  and 
when you are pointing to an array ,the first index's value will be stored into the pointer,have a happy coding       
                       
                                           

No comments:

Post a Comment