pointer to structure using (-> operator)
For Example: Write a program to demonstrate the use of pointer
to structure using (-> operator)
#include<stdio.h>
#include<conio.h>
struct student //here student is structure name
{
int roll_no; //members
char sname[20];
char sclass[20];
char city[20];
char dob[10];
float marks;
};
void main()
{
struct student
s,*ptr; //Here s is simple variable of
structure and *ptr is pointer variable
ptr=&s; //initialized pointer to structure variable
printf("\n Enter Roll Number: ");
scanf("%d",&s.roll_no);
printf("\n Enter Name of student:
");
scanf("%s",&s.sname);
printf("\n Enter Class of student:
");
scanf("%s",&s.sclass);
printf("\n Enter city: ");
scanf("%s",&s.city);
printf("Enter date of birth: ");
scanf("%s",&s.dob);
printf("Enter student marks: ");
scanf("%f",&s.marks);
printf("\n ----------------------");
printf("\n Students Details");
printf("\n
----------------------");
printf("\n Student Roll is:
%d",ptr->roll_no); // member access
using pointer variable
printf("\n Student Name is:
%s",ptr->sname);
printf("\n Class is:
%s",ptr->sclass);
printf("\n Student City is:
%s",ptr->city);
printf("\n Date of Birth is: %s",ptr->dob);
printf("\n Student Marks is: %f
",ptr->marks);
getch();
}
Output:
Comments
Post a Comment