You are on page 1of 9

Structures

When we use different data types under one name ,structures is


used.
General Format of a structure :Struct tag_name
{
Data_ type member_1;
Data_type member_2;
Data_type member_n;
};
EG.:
struct comp
{ char compname[20];
char worker[15];
int salary;
};

Accessing the structure members


when we need to access a data member via the
structure variable the dot(.) operator is used.
Dot(.) is used between the variable name and data
member when we have to access data members.
Dot operator is also known as PERIOD
OPERATOR.
Reading structures members:-scanf(%s,&comp.workername);
Scanf(%d,&comp.salary);

SAMPLE PROGRAM
#include<stdio.h>
int main()
{
struct comp
{
char workername;
char companyname;
int salary;
};
struct comp c1= {anuj,Ranbaxy,5550};
printf (Name=%u\n,c1.workername);
printf(company name=%u\n,c1.companyname);
printf(salary=%u\n,c1.salary);
return 0;
}
Output will be:
Name=anuj
Companyname=Ranbaxy
salary=5550

POINTERS TO
STRUCTURES
Pointers can be used with structures to make use
of address and
defining structures.
The notation used to pointer to structure is:
(*ptr).number

Example
#include<stdio.h>
#include<conio.h>
struct invent
{
char *name[20];
int number;
float price;
};
void main()
{
struct invent product[3], *ptr;
printf(INPUT\n\n);
for(ptr= product; ptr<product+3; ptr++)
scanf(%s%d%f,&ptr->name, &ptr->number, &ptr->price);
printf(\n OUTPUT\n\n);
ptr= product;
while(ptr < product+3)
{
printf (%-20s %5d %10.2f\n);
ptr->name;
ptr->number;

OUTPUT will be:--INPUT


Celling_fan 5 7500
Electric_oven 12 350
OUTPUT
Celling_fan 5 7500.00
Electric_oven 12 350.00

Function with structure


A structure variable can also be passed to a function. We may either pass
individual elements or entire structures. It is necessary to declare the structure
globally.
#include<stdio.h>
Struct student
{
char name[30];
int roll_no[30];
int cell_no;
};
void print_data(struct student);//function prototype
void main()
{
struct book b1=(akshat,03,9999999999);
print_data(b1);
}
void print_data(struct b)
{
printf(%s%d%d, b.name, b.roll_no, b.cellno);
}
Output will be:

Govt. Engg. College


Ajmer
SUBMITTED TO:DILIP SISODIYA SIR

SUBMITTED BY:Amit kumar swami


12eeacs005

THANK YOU

You might also like