Pages

Monday, November 22, 2010

Pointers and structures

Structures are user defined datatypes(refer to earlier posts for a detailed study). We can not only create pointers for variables which have an inbuilt data type but also for variables which have a structure  datatype. The declaration of such a pointer is same as of a normal pointer as evident from the declaration below

struct computer
{
    char processor[12];
    char make[10];
    short ram;
}com,*comp;



In the above code we have declared a structure name "computer" which has three member variables namely processor,make and ram. What these variables are for is not relevant for this topic. What is important is that we have declared 2 objects - com and *comp of the structure. The first object(com) is a simple object which can access the member variables of the structure using the dot (.) operator like - com.processor, com.make etc. but the second object is a pointer object. Just like an integer pointer can point to any integer, a structure pointer can point to any thing which is of structure type. In other words "*comp" can point to any variable/object which has "computer" as its datatype. You can see processor is of char type, so is make and ram is of int type. There is only one variable which has its datatype as "computer" and it is "com". So the pointer "*comp" can point to "com".

How it is done

Let's go through the complete code to see how it is done.


Example 1

struct computer
{
    char processor[12];
    char make[10];
    short ram;
}com,*comp;


void main()
{
    // Populating the com object of the structure computer using the "." operator
    printf("\nEnter Processor Type  ");
    scanf("%s",&com.processor);
    printf("\nEnter Company name ");
    scanf("%s",&com.make);
    printf("\nEnter Amount of Physical RAM (In GB) ");
    scanf("%d",&com.ram);

    comp=&com; // making comp point to com

    //Printing the data using the object
    printf("\nProcessor %s",com.processor);
    printf("\nCompany Name %s",com.make);
    printf("\nRAM In GB %d",com.ram);

    //Printing the data using the pointer
    printf("\nProcessor %s",comp->processor);
    printf("\nCompany Name %s",comp->make);
    printf("\nRAM In GB %d",comp->ram);

}

Description
The program has a structure named "computer" with three variable as members - processor,make and ram. The two object "com" and "*comp" are declared just after closing the structure declaration making them public for all the functions of the program. The "com" object is initialised by asking the values from the user and storing them in the object by using the "." operator. The pointer object (comp) comes into play only when the statement "comp=&com" is reached. This statement makes the pointer(comp) point to the object (com). Now that it is a pointer to the object it can access the values of the object. To access the value of the object the arrow operator(->) is used. It is made by pressing the hyphen or the minus("-") sign with the greater than(>) sign. Just as a normal object access the values of the structure using a dot operator, a pointer object accesses the values using the arrow operator.


Example 2

Using a structure pointer with a function

#include
#include
struct worker
{
    int work_code; // worker code
    char work_name[12];// name of the worker
    int work_sal;//salary of the worker
}work1,work2,*work_ptr;

void main()
{

    clrscr();
    void show_worker(worker *);
    printf("\nEnter First Worker's Data ");
    printf("\nEnter Code of The Worker ");
    scanf("%d",&work1.work_code);
    printf("\nEnter Name of The Worker ");
    scanf("%s",&work1.work_name);
    printf("\nEnter Salary of The Worker ");
    scanf("%d",&work1.work_sal);

    work_ptr=&work1;
    show_worker(work_ptr); //calling the function with the firstobject

    printf("\nEnter Second Worker's Data ");
    printf("\nEnter Code of The Worker ");
    scanf("%d",&work2.work_code);
    printf("\nEnter Name of The Worker ");
    scanf("%s",&work2.work_name);
    printf("\nEnter Salary of The Worker ");
    scanf("%d",&work2.work_sal);
    work_ptr=&work2;
    show_worker(work_ptr);//calling the function with the secondobject
}
void show_worker(worker *wptr)
{
    printf("\nWorker code : %d",wptr->work_code);
    printf("\nWorker name : %s",wptr->work_name);
    printf("\nWorker salary : %d",wptr->work_sal);
}

Description

The "worker" structure has 2 normal objects (work1 and work2) and a pointer object (work_ptr) which points to both the objects one by one. It first points to the first object (work1) in the statement - work_ptr=&work1. Once work_ptr has the address of work1 it is passed as an argument to the function (show_worker) which takes one argument of "worker *". The value of work_ptr is "copied" to "wptr" which is declared in the argument list of the function. At this point it can be said that since "work_ptr" has been copied to "wptr", both these pointers are now pointing to "work".  When the function is first called it prints the code,name and salary of the first worker since the pointer has the address of the first object. Similarly before the second call is made the pointer is pointed towards the second object in the statement "work_ptr=&work2". In the second call the value of work_ptr is again copied to wptr which this time prints the data of the second object.


0 comments:

Post a Comment