Search This Blog

Tuesday, April 21, 2020

Pointer full expalination C-Programming.

        C-Programming:-

In this tutorial you'll learn :-

Pointer:-

 

 

Pointer is a variable that stores the address of another variable . The pointer is represented by sign (  *  ) .

There are some features of pointer in C programming language are  :-

1. Pointer saves memory space .
2. Execution time with pointer is faster because data is manipulate with the address is direct access to memory location .
3. Pointer reduce length and complexity of the program .
4. 2D and 3D array and many more dimension array representation is easy in pointer .

Pointer declaration :-

Pointer declaration names a pointer variable and specifies the type of the object to which the variable point.

syntax :-              Data type               * pointer name  ;

for e,g:-

int x=15; // declaration of pointer
int *p ;
*p=&x;
so, here we accessing the pointer value.

Initializing the pointer variable :-

The process of assigning the add variable in a pointer variable is known as initialization of  a pointer variable.

  for e.g -  

int q=100 , *p ;
p=&q ;
so this is the initializing pointer.

Let see one example :-

#include<stdio.h>
int main()
{
int x=10 , *a ;
float y=20 , * b ;
a=&x;
b= &y;
printf(" a = %d \ n   b = %f",(*a),(*b));
return( 0 ) ;
}

OUTPUT

a=10
b=20

Pointer to pointer:-

Pointer to pointer, the first pointer contain the address of second pointer. let see below.


               Pointer                      pointer                             variable
       Value
Address
   Address



 

for e.g:-

#include<stdio.h>
int main()
{
int v=300 ;
int *ptr , *p ;
 p=&v ;
ptr = & p ;
printf(" value of v is = %d",v);
printf(" value of p is = %d", p);
printf(" value of ptr is = %d", ptr);
return( 0 ) ;
}

OUTPUT:-

value of v is= 300 
value of p is = 300
value of ptr is 300

Pointer and function :-

The pointer can be used as arguments in the function . The argument or parameters to the function are passed in two ways:-

* Call by value :-

The process of passing of actual value of variables is known as "call by value " . When a function is called in program , the values to the arguments in the function are taken by the calling program, the values taken  can be used inside the function. Alteration to the value isn't accepted inside the function in calling program but change is locally available in the function .

For example:-

// Write a program to swap the values stored in two variable using function with pointer.


#include<stdio.h>
void swap ( int  , int  );
int main( )
{
int a =10 , b =20 ;
printf("\n Before swapping a= %d  ,  b + %d", a , b );
swap ( a , b) ;
printf(" After swapping :  a =%d   b  = %d\n",a,b);
}
void swap ( int  a  , b )
{
int temp;
temp =  a;
a = b;
b = temp;
}

OUTPUT:-

Before swapping   a = 10  , b = 20
After swapping     a = 10  ,  b =20

* Call by reference:-

The process of calling a function using pointer to pass the address of variable is known as call by reference . A function can be declared with pointer as it's arguments.
The address of the variable are altered to the pointer and any changes made to the value inside the function is automatically carried out in the location. This change is accepted by the calling function.

For example:-

// Write a program to swap the values stored in two variable using function with pointer.

#include<stdio.h>
void swap ( int * , int * );
int main( )
{
int a =10 , b =20 ;
printf("\n Before swapping a= %d  ,  b + %d", a , b );
swap ( &a , &b) ;
printf(" After swapping :  a =%d   b  = %d\n",a,b);
}
void swap ( int * a  , *b )
{
int temp;
temp =  *a;
*a = *b;
*b = temp;
}

OUTPUT:-

Before swapping   a = 10  , b = 20
After swapping     a = 20  ,  b = 10

  Pointer and array :-

* Array is a collection of similar data type element stored under common name .
* The element of an array can be efficently accessed by using pointer.
* The base address of array  start with zero element of the array.

for e.g:-

int   a[5] = { 10 , 20 , 30 , 40 , 50 };
 element     =       a[0]        a[1]       a[2]           a[3]            a[4]
value          =       10           20           30             40              50  
address      =      1000        1002      1004       1006          1008

Relation between pointer and array:-

An array is a block of  sequential data , where as  pointer ois a variable which holds the base address of an array so there are a relation between an array and pointer are :-

let see one example:-

//Write a program to print a address of array element.

#include<stdio.h>
int main()
{
int x[5] , i ;
for(i=0 ; i<5 ; i++)
 {
printf("address of an array x is =%p", x);
return (0);
}

Another example:-

#include<stdio.h>
int main ()
{
int x[5] = { 1 , 2 , 3 , 4 , 5 } ;
int *p ;
p=&a[1] ;
printf("%d", *p);
printf("%d",*(p+1)) ;
printf("%d", *( p+2-1) ;
return(0) ;
}

OUTPUT

3
4
4

Formula of calculating address of an array using pointer :-

Address of an array element is a calculating using it's index and the scale factor of data type.

scale factor= size of data type in bit.

 int    arr [3] = ( base address ) + ( 3*scale factor ) .

Array of pointer:-

The array element can be accessed by two method :-
1. Standard array notation.
2. Pointer arithmetic .
A collection od address or collection of pointers .

Advantages of an array of pointer :-

1. Accessing arrays through pointer is marginally faster than using array notation 
2. Pointer gives better control over array processing

For e.g:-

include<stdio.h>
int main ()
{
int *p[5] ;
int a=10 , b=20 , c=30 , d= 40 , e= 50 , i ;
p[0] = &a ;
p[1] = &b ;
p[2] = &c ;
p[3] = &d ;
p[4] = &e;
 for( i=0 ; i<5 ; i++)
{
printf(" address  =%d \ t value = % \ n ", p[i] , * p[i] );
}
return ( 0 ) ;
}

Pointer and structure :-

This topic carry a new topic STRUCTURE, that we are not know how to use this so we learn this topic in our upcoming post structure and union .

Pointer and string :-

A character pointer is a pointer to the character . It can be declared as :-
char   * pointer character ;
A character pointer  can also be used to access character array and string constant in  the same way as accessing numeric arrays using their respective pointer variable .

Let see one example :-

'// Write a program to input any string using pointer .

#include<stdio.h>
int main()
{
char name [ ] = " codeswithabhi " ;
char *p ;
p=name ;
while ( *p != '\0' )
{
printf("%c",*p);
p++;
}
return(0);
}

OUTPUT 

codeswithabhi 

Thanks for reading ...........

Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

1 comments:

If you have any doubt, please let me know