Wednesday 28 February 2018

Pointer In C

Pointer is a variable that points to an address of a value.
Pointers are used to access the memory and manipulate the address.
Pointer is used to allocate memory dynamically i.e. at run time. 
Pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
Reference operator (&) and Dereference operator (*)
& is called as reference operator (address of operator) which gives you the address of a variable.
(*) is called as dereference operator (indirection operator) which gives value from the address.
Advantage
Pointer reduces the code and improves the performance.
Uses:
We can dynamically allocate memory using malloc() and calloc() functions in pointer
Pointers are used in arrays, functions and structures. It reduces the code and improves the performance.
Syntax :
data_type *var_name;

Example 1:
 int *p;
 char *p;
Where * is used to denote that p is pointer variable .
Example 2:
 int x=5;
 int *p;
 p=&x


Important Points:
1.     Pointer variable stores the address of the variable.
2.    & symbol is used to get the address of the variable and *symbol is used to get the value of the variable that the pointer is pointing to.
3.  Always C pointer is initialized to null, i.e. int *p = null.The value of null pointer is 0.
4.     If a pointer in C is assigned to NULL, it means it is pointing to nothing.
5.     The size of any pointer is 2 byte (for 16 bit compiler).
6.     %d(%i) int signed integer
7.     %u int unsigned decimal
8.     %x (%X) int unsigned hex value
9.     %p pointer address stored in pointer

Example:
#include<stdio.h> 
#include<conio.h>
void main(){ 
int  x=50;
clrscr();
 int *p;     
 p=&x;//stores the address of number variable   
  printf("Value of variable var is: %d\n", x);
   printf("Value of variable var is: %d\n", *p);
   printf("Address of variable var is: %x\n", &x);
   printf("Address of variable var is: %x\n", p);
   printf("Address of pointer p is: %x", &p);
  getch();
}   

Output:

Value of variable var is: 50                                                                                                   
Value of variable var is: 50                                                                                                   
Address of variable var is: cafdf484                                                                                           
Address of variable var is: cafdf484                                                                                           
Address of pointer p is: cafdf488   

Pointer to a Pointer (Double Pointer)

When one pointer variable stores the address of another pointer variable, it is known as Pointer to Pointer  or Double Pointer.
Syntax:
Data_type ** variable_name;
Example:
int **p1;
Here we have used two indirection operator(*) which stores and points to the address of a pointer variable i.e, int.

Example 1:
#include <stdio.h>
#include<conio.h>
void main() {
   int  a = 20;
   clrscr();
    int  *p1;       //It can store the address of variable a
    int  **p2;  //  It can store the address of pointer variable p1 only.
    p1 = &a;
    p2 = &p1;
    printf("Address of a = %u\n", &a);
    printf("Address of p1 = %u\n", &p1);
    printf("Address of p2 = %u\n\n", &p2);
    printf("Value at the address stored by p2 = %u\n", *p2);
    printf("Value at the address stored by p1 = %d\n\n", *p1);
    printf("Value of **p2 = %d\n", **p2);
   getch();
}
Output:
Address of a = 3576370748                                                                                                      
Address of p1 = 3576370752                                                                                                     
Address of p2 = 3576370760                                                                                                     
                                                                                                                            Value at the address stored by p2 = 3576370748                                                                                 
Value at the address stored by p1 = 20

Value of **p2 = 20 

Example 2:
#include<stdio.h> 
#include<conio.h>
void main(){ 
int num=60;
clrscr();    
int *p;//pointer to int   
int **p2;//pointer to pointer       
p=&num; //stores the address of number variable     
p2=&p;   
printf("Address of number variable is %x \n",&num);     
printf("Address of p variable is %x \n",p);     
printf("Value of *p variable is %d \n",*p);      
printf("Address of p2 variable is %x \n",p2);     
printf("Value of **p2 variable is %d \n",*p);     
getch();
} 
 Output
Address of number variable is b87c3a4                                                                                          
Address of p variable is b87c3a4                                                                                               
Value of *p variable is 60                                                                                                     
Address of p2 variable is b87c3a8                                                                                              
Value of **p2 variable is 60 

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...