C Language | Structure and Union: Structures And Pointers

Structures And Pointers

In order to fetch data elements using a normal structure variable we use. (dot) operator.

 Whereas 

to fetch data elements using a pointer type structure variable we use -> (arrow) operator.

Question:1
C program to declare a structure named student with attributes as roll, name and per. Further display the details.
Sol:

#include<stdio.h>
#include<conio.h>
struct student
{
  int roll;
  char name[20];
  float per;
};

int main()
{
  struct student s1,*s;
  s=&s1;
  printf("Enter roll no ");
  scanf("%d",&s->roll);
  printf("Enter name ");
  scanf("%s",s->name);
  printf("Enter per ");
  scanf("%f",&s->per);
  printf("rolll no %d name %s  per %f\n",s->roll,s->name,s->per);
  getch();
  return(0);
}
/* Output */
Enter roll no 105
Enter name Kapil
Enter per 98
rolll no 105 name Kapil  per 98.000000

Question:2
C program to declare a structure named employee with attributes as empno, name and salary. Further display the details.
Sol:

#include<stdio.h>
#include<conio.h>
struct employee
{
  int empno;
  char name[20];
  float sal;
};

int main()
{
  struct employee p1,*p;
  p=&p1;
  printf("Enter empno ");
  scanf("%d",&p->empno);
  printf("Enter name ");
  scanf("%s",p->name);
  printf("Enter salary ");
  scanf("%f",&p->sal);
  printf("empno %d name %s  sal %f\n",p->empno,p->name,p->sal);
  getch();
  return(0);
}
/* Output */
Enter empno 1001
Enter name Amit
Enter salary 45000
empno 1001 name Amit  sal 45000.000000

Question:3
C program to declare a structure named product with attributes as product no, product name, rate, quantity and cost. Take input for the details calculate and display total cost.
Sol:

#include<stdio.h>
#include<conio.h>
struct product
{
  int pno;
  char pname[20];
  float rate,qty,cost;
};

int main()
{
  struct product p1,*p;
  p=&p1;
  printf("Enter product no ");
  scanf("%d",&p->pno);
  printf("Enter product name ");
  scanf("%s",p->pname);
  printf("Enter rate and qty  ");
  scanf("%f %f",&p->rate,&p->qty);
  p->cost=p->rate*p->qty;
  printf("pno no %d pname %s  cost %.2f \n",p->pno,p->pname,p->cost);
  getch();
  return(0);
}
/* Output */
Enter product no 101
Enter product name Lux
Enter rate and qty  45
6
pno no 101 pname Lux  cost 270.00

Question:4
C program to declare a structure named student with attributes as roll no, name, marks of three subjects (m1,m2,m3), total and per.Take input for the details calculate and display total and per.
Sol:

#include<stdio.h>
#include<conio.h>
struct student
{
  int roll;
  char name[20];
  float m1,m2,m3,total,per;
};

int main()
{
  struct student s1,*s;
  s=&s1;
  printf("Enter roll no ");
  scanf("%d",&s->roll);
  printf("Enter name ");
  scanf("%s",s->name);
  printf("Enter marke of m1,m2 and m3 ");
  scanf("%f %f %f",&s->m1,&s->m2,&s->m3);
  s->total=s->m1+s->m2+s->m3;
  s->per=s->per=s->total/3;
  printf("rolll no %d name %s  Total %f Per %f\n",s->roll,s->name,s->total,s->per);
  getch();
  return(0);
}
/* Output */
Enter roll no 101
Enter name Ankit
Enter marke of m1,m2 and m3 98
97
95
rolll no 101 name Ankit  Total 290.000000 Per 96.666664

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects