Showing posts with label Pointer to structure. Show all posts
Showing posts with label Pointer to structure. Show all posts

Friday 30 March 2012

Pointer to structure


Pointer to structure

The use of structure define the different data type as a bunch of code.The pointer to structure and array of structure to create structure dynamically at run time.

Declaring structure and structure to ppointer and accessing its member variable is as follow:
Ex,
          struct data
            {
                   int num;
                   char name[20];
                   float salary;
          };
          struct data d,*p;
          p=&d;
          // accessing member
          p->num=10;
          strcpy(p->name,”Om”);
          p->salary=5000;
The symbol-> is called arraow operator (member selection operator) is used to access the member of structure when accessed by pointer.
Ex,
#include<stdio.h>
#include<conio.h>

struct emp
{
          int no;
          char name[15];
          long int salary;
};

void main()
{    //initialization
          struct emp *e;
          clrscr();

          printf("\n enter no     : ");
          scanf("%d",&e->no);
          fflush(stdin);
          printf("\n Enter name   : ");
          gets(e->name);
          printf("\n Enter salary : ");
          scanf("%ld",&e->salary);

          printf("\n No         Name           Salary");
          printf("\n %d   %s              %ld",e->no,e->name,e->salary);
          getch();
}

Posted By : Ruchita Pandya