POINTERS TO STRUCTURES


POINTERS TO STRUCTURES:

We know that pointer is a variable which holds address of a variable (int or float or any datatype )or address of a function.
            Similarly a pointer to a structure can also be defined which holds the base address of structure member variable.
           
A pointer to a structure can be declared as follows:

struct tagname *variable;

Eg.,
struct book
{
char name[10];
int pages;
float price;
};
struct book *ptr;
           
The members of the structure can be accessed through pointer variable using arrow operator( -> )which is also known as member selection operator.
           
The members of the structure book can be accessed as:
ptr->name
ptr->pages
ptr->price

Ø  A Program on pointers to structures

void main()
{
struct student
{
char name[10];
int rollno;
float avg;
};
struct student s1={“Ajay”,5,80.5};
struct student *ptr;
ptr=&s1;
printf(“name=%s\n”ptr->name);
printf(“rollno=%d\n”,ptr->rollno);
printf(“avg=%f”,ptr->avg);
}

No comments:

Post a Comment