Monday, April 8, 2013

Structure pointer as the function argument

Generally structures are used to store different data types. sometimes we need to pass the structure as the parameter to the function. Arguments are stored in the stack. If the size of the structure is very large then storing that on stack becomes hard and data accessing becomes slow. so we can pass the pointer to the structure as the parameter instead of the structure. This enables the faster data access.

consider the structure

struct data
{
// All the data
};

For this declare a pointer. Structure is also a data type so we can create a pointer to this as the same way we create for the int ,char etc.
for integer we create pointer as int *p=NULL;
p is the pointer of type integer. which always points the integer type variables.
Similarly we can create pointers to the structure.
struct data *dt=NULL;
Here dt is the pointer to the user defined data type data. To access the structure variables we have to use arrow operator ->.

struct var
{
int a;
char c;
};

let p is the pointer to the structure var. To access the integer variable we need statement like var->a.
Generally allocating data to the pointer is good before using it. If we are using the pointer in the same function no need of allotting memory. If we need to use the same pointer f to pass data between functions we need to allot memory using dynamic memory allocation techniques like malloc , calloc in C language and by using new in C++.
We need memory allocation because after returning from functions all the variables initialised and used are deleted from heap except if the variable is used with memory allocation.

var* function1(void)
{
struct var *p=NULL;
p=(struct var*)malloc(sizeof(struct var));;
p->a=10;
p->c="A";
return p;
}

void function2(var* p)
{
int r=p->a;
printf("\n Value of r is %d",r);
}

Here we are using the structure explained above. we are passing structure as pointer from one function to another function. and in the second function we just printing the value of the integer a. We are allotting the memory to pointer dynamically. This is C type memory allocation.
The C++ type allocation is as fallows we need to modify the second statement of the function1.


var* function1(void)
{
struct var *p=NULL;
p=new var;
p->a=10;
p->c="A";
return p;
}

some times we need to hide the type of pointer like we need to pass the structure but the functions should not identify the pointer as pointer to structure. To achieve this we can use void pointers. Just it is casting of pointers.

consider the void pointer void* ptr1=NULL;
strcut var *ptr2=NULL;
ptr2=new var;
Here ptr1 and ptr2 are the two pointers pointing void and structure respectively.

ptr1=(void*)ptr2;


Here just casting operation is done. Now consider the previous example where we are passing pointer to structure as argument. Modify that to pass structure pointer as void pointer.


void* function1(void)
{
struct var *p=NULL;
p=new var;
p->a=10;
p->c="A";
void *ptr=NULL;
ptr=(void*)p;
return ptr;
}
 In the second function we need to convert the void pointer back to pointer to structure. Then second function looks like

void function2(void *vptr)
{
struct var *sptr=NULL;
sptr=(struct var *)vptr;
int r=sptr->a;
printf("\n Value of r is %d",r);
}

No comments:

Post a Comment

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...