Featured Post

Tuesday, June 28, 2016

Pointers in C

                                                          Pointers in C


   Simply a pointer is variable which holds the address of another variable.
   Then What is the differnce between the a normal variable and pointer variable?
   What happens when we declare the variable like

      int a=34;
     
  The above declaration tells the compiler that 'a' is a integer variable and reserves two bytes of memory for 'a'.
   Now the question is where the 'a' is in memory?
   
      well do u remember the scanf function?

    scanf("%d",&a);
 
in scanf fucntion there is a symbol '&' nothing but it represents the address of the variable 'a' in memory(for example 1024 address)

While using pointers we use * and &

 declaration of pointer variable:
      datatype * variable=value;
     here datatype is int float or char or even void data type
   
     for example int *a;
     here 'a' is a integer pointer i.e, it can store only integer address
     
    int *b;
    int c=10;
    b=&c;
     now b conatins the address of c.
   
    /*pointers in c*/
   
     main()
     {
      int *p;
      int q=20;/*pointer variable declaration*/
      p=&q;
      printf("\nvalue of q is %d ",q);
      printf("\nvalue of q is %d ",*(&q));
      printf("\nvalue of q is %d ",*p);
      printf("\nAddress of q is %u ",p);
      printf("\nAddress of q is %u ",&q);
     }
    output:
        value of q is 20
        value of q is 20
        value of q is 20
        address of q is 1190
        address of q is 1190


/*  swapping of two numbers using pointers*/

    void swap(int *,int *);
    void main()
    {
       int a=5,b=6;
       printf("before swapping a=%d\tb=%d",a,b);
      swap(&a,&b);
      printf("after swapping a=%d\tb=%d",a,b);
   }
    void swap(int *p,int *q)
   {
     int temp;
   temp=*p;
   *p=*q;
   *q=temp;
   printf("\m after swapping a=%d\tb=%d",a,b);
   }

  output:
   before swapping a=5      b=6
    after swapping a=6      b=5
 
   
                                             phython programming

Pointers in c, c programming using pointers, pointer declaration, pointer initialization void pointer integer pointer character pointer float pointer swapping of two numbers using pointers. Advantages of pointers. pointer arithmetic copy of one string to another string using pointers reverse of a string using pointers



   

No comments:

Post a Comment