Professional Documents
Culture Documents
Presentation Struct in C 1443293894 140676
Presentation Struct in C 1443293894 140676
Define Structure
Create Structure variable
Access structure member using . operator
Array of Structures, passing structure to functions
Nested Structures
Structure are collection of different datatype
grouped together under a single variable name for
convienient handling. 'struct' is a keyword, struct type
is a name which identifies the structure.
syntax : struct <structure name or tagname>
{
datatype member1;
datatype member2;
};
struct book
{
int pages;
char bookname[10];
char author[20];
float price;
};
J A V A H A U N T E R 467
Syntax :
struct structurename variable1, variable2, ..... variable n;
Example
Struct book b1; /* b1 is a structure variable name */
Syntax :
Stru_variablename.membername;
Example
B1.pages=400;
B1.price=450.00;
Write a Program the create the Account Details using Structure?
#include<stdio.h>
struct amount
{
int ac_no;
char name[10];
int balance;
};
void main()
{
struct amount v;
printf("\nEnter the account Details");
scanf("%d%s%d",&v.ac_no,&v.name, &v.balance);
printf("The values are %d%s%d",v.ac_no,
v.name, v.balance);
printf("%d",sizeof(struct amount));
}
Write a program to create student structure
#include<stdio.h>
struct stud
{
int id;
char name[20];
int mark1,mark2,mark3;
int total;
int avg;
}b;
void main()
{
printf("\nEnter the student details");
scanf("%d %s %d %d %d",&b.id,&b.name,
&b.mark1,&b.mark2,&b.mark3);
b.total=b.mark1+b.mark2+b.mark3;
b.avg=b.total/3;
printf("%d %s %d %d %d",b.id,b.name, b.mark1, b.mark2,
b.mark3);
}
Array of structures are defined as a group of data of
different data types stored in a consecutive memory location with
a common variable name.
For Example,
struct employee
{
int empno;
char empname[20];
char deptname[10];
float salary;
}emp[5];
Passing Structures as Arguments
A structure variable can be passed as an argument to
another function
Example
Struct date
{
int month;
int day;
int year;
};
struct account
{
int acc_no;
char name[40];
char acc_type;
float balance;
struct date dob;
} customer;
/* NESTED STRUCTURES
A structure within another
scanf("%d",&s.sno);
structure */
printf("enter the sname\n");
#include<stdio.h>
scanf("%s",&s.name);
struct dob {
printf("enter the dob\n");
int date;
scanf("%d%d%d %c",&s.sdob.date,
int month;
&s.sdob.month,&s.sdob.year,&s.sex);
int year; };
printf("%d\t %s\t %d %d %d \t
struct stud{
%c", s.sno, s.sname, s.sdob.date,
int sno;
s.sdob.month, s.sdob.year, s.sex);
char sname[10];
getch();
struct dob sdob;
}
char sex; };
void main()
{
struct stud s;
clrscr();
printf("enter the sno\n");
Session Summary
The keyword “struct” begins the structure definition followed by the tag(i.e strcture
name) within the braces the structure members are declared
The structure definition create a new data type that can be used to declare variables.
A member of structure is always referred and accessed using the member access
operator(.) via the structure variable name
A structure containing a member that is a pointer to the same structure type is called
as a self referential structure.
The structure pointer operator () is used when accessing a member of a structure via
a pointer to the structure
EXERCISES
2. Define a structure called student that will describe the following information student
name,class,rollno, subject marks & total. Using student declare an array stu_list
with 30 elements. Write program in C to read the information about all the 30
students and to display the information?