Sunday, September 30, 2012

Pointer's

You already know that pointer is most difficult topic in c and c++ languages here i will tell you what is pointer and how you can use pointer with arrays,functions and structures etc.

What is pointer :- Pointer is the user defined data type which can hold the address of simple data type like int,char,float and user define data type   like function,pointer etc. and derived data type like array,structure and union. pointer is denoted by *   eg :- int *x;

Before pointer we will discus what are simple variables like int,float and char etc. how simple variables are stored in computer memory.

Example :- int x =10;

                                                                   x
           10          
                                                                   0x5278

Suppose above box is block of computer memory.
Above fig. is showing that how 10 is stored in memory.
What is x?
What is 10?
What is 0x5278?
0x5278 is the address where 10 is stored and x is the name of address (or location) and 10 is the value of x or value at that address.

Pointer declaration  :- How pointer is declared ?
1. int *iptr;
2. char *cptr;
3. float *fptr;
Here iptr is a pointer variable which can hold the address of int type similarly cptr can hold the address of char type and fptr can hold float type.

How pointer is used :-
int x = 20;
int *iptr;
iptr = &x;

--In first statement simple variable is declared.
--In second statement pointer variable is declared.
--In third statement address of value 10 is assigned to iptr means now value of iptr is the address of the value 10 that is 0x5278.
                                                               
                                                                  x

           20            
                                                                  0x5278

                                                                  iptr
        0x5278       
                                                                  0x3678

0x3678 is the address where address of x is stored having name iptr.
Now how we will access the value of x by using iptr?
printf("%d\n",*iptr);
above statement will print the value of x means the output will be 10
Note :- int type pointer will store the address of only int type variable you can't store the address of float,char into int type pointer......
int x = 10;
float *fptr;
fptr = &x;
Third statement is wrong because x is an int type and fptr is a float type and you can't assign the address of int to float type pointer .
But if you want to do this then you will first typecast it.
Important fact about pointer :- 
                                    *&p = p;
 But &*p = p will not compile.
----------------------------------------------------------------------------------------------------------------------------------
Pointer with arrays :-

Array :- Array is a derived data type it is used  to store the homogeneous values and the main fact is arrays are used to allocate the contiguous memory.
Example :- int arr[10] = {10,20,30,40,50,60,70,80,90,100};
above statement will reserve the 20 bytes of contiguous memory. because int is of 2 bytes in 16 bit compiler.
array structure :-
assume the base address is 0x5270

                    arr         
   10       20       30       40      50      60      70      80      90      100   
                    0x5270
What is arr?
What is 0x5270?
What is 10,20,30------100;
0x5270 is the base address of the whole array and arr is the name given to the base address and 10,20-----100 are values of the arr. By using base address you can access the whole values of the arr 
if you will print the arr then it will print the base address
Example :-  printf("%p\n",arr);
output :- 0x5270
1. arr;
2. &arr;
3. arr[0];
Above three statements will give the same value that is the base address 0x5270.
Note :- you can't change the base address. You can't  perform addition, subtraction, multiplication and division with base address.
Example :- arr = arr + 1;
                   arr = arr - 1;
                   arr = arr * 2;
                   arr = arr / 5;
Above all statements are wrong because if you will change the base address then there is no way to access the previous values(elements) of array.

Note :- Array is a constant pointer You can't change the array's base address.

Access the whole array elements using a pointer variable :- 
Example:- 
Int *ptr;
Int arr[10] = {10,20,30,40,50,60,70,80,90,100};
ptr = &array[0];            //  assign base address of "arr" to "ptr"

Assume base address is 0x5270


                    arr
   10       20       30       40      50      60      70      80      90      100   
                    0x5270


                                                                    ptr
     0x5270     
                                                                    0x6488


Above Fig. shows how pointer variable "ptr" holds base address of an array "arr"
Now you can access the all elements of "arr" using pointer variable "ptr"  like
1. printf("%d",*(ptr+i));   or        //  "*" means "value at address"
2. printf("%d",ptr[i]);
Above both statements will print the all elements of array "arr"

----------------------------------------------------------------------------------------------------------------------------------
Pointer with functions :- 

Function :- Function is build of three things.
1. Function declaration.
2. Function call.
3. Function definition.
Example :- 
// parameterized function with return value.
#include<stdio.h>
int sum(int,int);           // Function declaration
void main()
{
         int x = 10;
         int y = 20;
         int c;
         c = sum(x,y);        //Function call
         printf("%d\n",c);
         return 0;
}
int sum(int a,int b)    // Function definition
{
          int z;
          z = a+b;
          return(z);      // Return statement
}
Output :- 30

Pointer with function :-
Using pointer we can pass the address of the variable
Example :-

#include<stdio.h>
int sum(int *,int *);       // pointer type parameters
void main()
{
         int x = 10;
         int y = 20;
         int c;
         c = sum(&x,&y);  // passing address of x and y
         printf("%d",c));
         return 0;
}
int sum(int *a,int *b)   // Now address of x and y is in a and b
{
          int z;
          z = (*a)+(*b);      //Adding "value at a" and "value at b".
          return(z);             //Returning sum of "value at a" and "value at b"
}

Note :- Only using pointer you can return more than one values.


Post a Comment