Sunday 27 November 2016

Use of Pointers

How use of pointer increases Execution Speed?

It is says that use of pointers will increase the speed of execution. Well it depends on the how and where we are using it.  When we use a pointer to access a variable then first program goes to the memory address of variable and then goes to variable. We will understand the execution time by taking two cases in consideration. First with passing single variable to a function and second with passing array of data to function.

Case 1: Passing Single variable to another function
Figure below show an example of finding square of a number by call by reference function :

      int value1=50;               //value1 defination
      int *ptr1;                     // pointer defination
      int value2=0;                          //value2 defination

      ptr1=&value1;               // copy value1 address to pointer ptr1
      value2=fuction1(ptr1);     // call of function for square a value
.
.
     /***function for square of a number*****/
     int function1(int *pointer)
    {int a=0;
     a=*pointer;
     return(a*a);}

value1 address will be stored in pointer ptr1. Square of a no is found by passing the value1 to function1 using call by reference. Function1 will calculate the square of the no and return the value to value2. I this before calling function1 cpu calculate the address of value1 and pass it to function. In function1 cpu first go to address, then find value at this location and then calculate the square of number. 
If we execute this function using call by value then program will be like this:

    int value1=50;               //value1 defination
    int value2=0;                          //value2 defination
    value2=fuction1(value1);  // call of function for square a value
    .
    .
    /***function for square of a number*****/
   int function1(int value)
  {
    return(value*value);}

It will transfer the value directly to function1. In function1 value1 will be copied to value, calculate the square of no and then return value to value2.
So call by value will take less time to execute this and program size will be small. So for such type of operation call by value method will be preferred.
Case 2: Passing array of data to function.
Figure below show the example of passing an array of data to function and then return average of array data using call by value:

int data[5]={50,40,34,23,78};           //5 byte array defination
int avg_value=0;                            // avg_value variable decleration
avg_value = avg_fun(data[0], data[1],data[2],data[3],data[4]);                                                                //function call by value     
.
.
/***function for average of a number*****/
int function1(int data1, int data2, int data3,int data4, int data5)
{
Int avg=0;
avg=(data1+data2+data3+data4+data5)/5;
return(avg);
}

As per above program if we pass multiple of data from one function to another function it will copied individually in the different location in stack. CPU accesses the data from the stack memory and takes average of data and return result to avg_data.
If we execute this function using pointer it will be like this:

int data[5]={50,40,34,23,78};           //5 byte array definition
int *ptr1;
int avg_value=0;                            // avg_value variable declaration
ptr1=&data;
avg_value = avg_fun(ptr1);                    //function call by value
.
.
/***function for average of a number*****/
int avg_fun (int *ptr1)
{
int avg=0;
int index=0;
for(index=0;index<5;index++)
avg=avg + *ptr++;
return(avg);}

In this scenario we only pass the starting address of data array. This data array starting address will be stored in stack memory. Cpu will fetch the data from location using pointers, calculate the average and return the result. So execution time will be less as compare to   call by value procedure. Another advantage of using pointer is that RAM memory used will be less. 


Saturday 26 November 2016

What is Pointer in C?


Pointers are the fundamental part of C language. In embedded system programming pointer plays a major role in embedded system programming. For example dynamic memory allocation is possible only by using of pointers.  So it is necessary to understand pointers base and use it in embedded system programming
Every variable that we declare will save on some memory location and its address is defined. This address of variable can be accessed using ampersand (&) operator. This operator will give memory address of variable.  For example:
Address1= &Value1;       // this statement assign the address of                                                  //value1 to Address1.

Here Adress1 will contain the address of Value1. Here 30 is the I value of Varaible1 and 4005 will be L value of variable.
So pointer is variable which holds the memory address of another variable. Pointers are used to access the memories and manipulate the address. Pointers play a main role in C programming. I will gives you access the memory location and manipulate them. If you want to pass a huge amount of data in to a function, it is easier to pass its address location to function then to copy every element of the  data. Moreover, if you need more memory for your program then you can allocate more memory using dynamic memory allocation.
Syntax of pointer declaration is as follows:
Data_type  *pointer_name;
Here Data_type is the type of variable for which it will be to be used. It must be as valid C data type.
“*” called as  dereference operator, this will gives you value at address. 
pointer_name will be name of the pointer.

Why data type of Pointer is required?

As we studied pointer is used to access variable using its address i.e. pointer will store the address of variables. We declare pointer according to variables data types. If we declare a pointer to integer variable data type of pointer will be integer.  The size of integer is 2 byte. It will take 16 bit to store integer value. So pointer data type of integer will be an integer. So if we access integer array of data using pointers then to access next value in any pointer increment will be of two memory location for 8-bit controllers. Same will follow for char (8-Byte) and float (4-Byte). Example of pointer declarations:
int variable1=0;                         // definition of variable1 as integer                                                    //type.        
int *pointer1=&variable1;       // integer pointer declaration  for                        //variable1 and copies the address of                  //variable1 in poiter1
char variable2=0;                    // definition of variable2 as char type.            
int *pointer2;                           // character pointer declaration  for variable1
float variable3=0;                    // definition of variable3 as float type.            
int *pointer3;                           // character pointer declaration  for variable

Accessing data using pointer

There are mainly three steps to use pointers:
  1. Declaration of pointer variable
  2. Assigning the address of variable to pointer variable
  3. Accessing the value at address stored in pointer variable.
For example:
int data1=4, data2;                    // definition of data1 ad data2
int *pointer_to_data1;              // pointer variable definition                                                               // for data1
pointer_to_data1 = &data1;     //assigning data1 address to                                                                //poiter_to_data1

data2=*poiter_to_data1;         // this will copies the data1 value in           //data1, data2 will //contain 4