0% found this document useful (0 votes)
10 views10 pages

structure-unit-4 (1)

The document provides a comprehensive overview of structures, unions, and enumerated data types in C programming. It includes definitions, syntax for declaration and initialization, examples of usage, and differences between structures and unions. Additionally, it discusses nested structures, arrays of structures, and methods for passing structures to functions.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

structure-unit-4 (1)

The document provides a comprehensive overview of structures, unions, and enumerated data types in C programming. It includes definitions, syntax for declaration and initialization, examples of usage, and differences between structures and unions. Additionally, it discusses nested structures, arrays of structures, and methods for passing structures to functions.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Structures-unions-enumerated data types

Q) Definition of structure:
A structure is a collection of different data types.
Q) Declaration and initialization of structure:
 A structure is declared using the keyword “struct” keyword.
 All variables of the structure are declared within the structure.
 These variables are called members of the structure.
Syntax:
struct structure_name
{
datatype member1;
datatype memeber2
datatype member n;
};
Example:

struct student
{
int rollno;
char name[25];
float totalmark;
};

Q) Declaration of structure variable:

When a structure is defined, it creates a user-defined type but, no storage is allocated. For the
above structure of student, variable can be declared as:

struct student stud1,stud2;

Thus, the stud1 and stud2 are structure variables of type student. The above structure can hold
information of 2 students.

It is possible to combine the declaration of structure combination with that of the structure variables,
as shown below.

1
struct student
{
int rollno;
char name[25];
float totalmark;
}stu1,stu2;

Q) Initialization of structure variables:

It means assigning some constant values to the members of the structure.


The general form is
struct stucture_name var={val1,val2,val3…..};

Example: struct student stud1={"Ashraf",1,98};

Accessing members of a structure


Each member of a structure is accessed by ‘ . ‘(dot) operator.
Syntax: structure_variaable. Memberneme;
Example:
stu1.name=”sai’;
stu1.no=10;
stu1.mark=100;

Example for student structure:


#include <stdio.h>
#include<conio.h>
void main()
{
struct student
{
char name[10];
int rollno;
int mark;
};
struct student stu1;
clrscr();
printf(“enter the student name,number marks\n”);
2
scanf(“%s%d%d”,&stu1.name,&stu1.rollno,&stu1.marks);
printf(“student details are\n”);
printf(“%s%d%d”,stu1.name,stu1.rollno,stu1.marks);
getch();
}
Q) Nested structures:
A structure that contains another structure as its member is called a nested structure.
#include <stdio.h>
#include <conio.h>
void main()
{
struct dob
{
int day;
int month;
int year;
};
struct student
{
struct dob d;
int rollno;
char name[25];
int mark;
}stud;
int n,i;
clrscr();
printf("\nEnter details of %d-th student\n”);
printf("\nName:\n");
scanf("%s",&stud.name);
printf("\nRoll number:\n");
scanf("%d",&stud.rollno);
printf("\nTotal mark:\n");
scanf("%d",&stud.mark);
printf("\nDate of birth (Format:01 06 2010):\n");
scanf("%d%d%d",&stud.d.day,&stud.d.month,&stud.d.year);
printf("\n\nRoll number:%d\n\n",stud.rollno);
3
printf("Name:%s\n\n",stud.name);
printf("Totel mark:%d\n\n",stud.mark);
printf("Date of birth:%d / %d / %d \n\n",stud.d.day,stud.d.month,stud.d.year);
}
getch();
}
Q) Arras of structures:
 Array of structures is nothing but collection of structures.
 Structure is used to store the information of one particular object but if we need to store such
100 objects then Array of Structure is used.
Syntax:
struct struct_name
{
type element 1;
type element 2;
……………..
type element n;
}array name[size];
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
struct student
{
int rollno;
char name[25];
int totalmark;
}stud[100];
int n,i;
clrscr();
printf("Enter total number of students\n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter details of %d-th student\n",i+1);
4
printf("Name:\n");
scanf("%s",&stud[i].name);
printf("Roll number:\n");
scanf("%d",&stud[i].rollno);
printf("Total mark:\n");
scanf("%d",&stud[i].totalmark);
}
printf("STUDENTS DETAILS:\n");
for(i=0;i<n;i++)
{
printf("\nRoll number:%d\n",stud[i].rollno);
printf("Name:%s\n",stud[i].name);
printf("Totel mark:%d\n",stud[i].totalmark);
}
getch();
return 0;
}
Q) Structures and functions:
A function may pass the members of structures in 3 ways.

Passing individual
members

Passing structures to Passing the entire


functions structure

Passing the address


of the structure

1) Passing individual members of a structure:


To pass any individual member of the structure to a function, we must use the direct selection
operator to refer to the individual members for the actual parameters.
Example:
5
#include<stdio.h>
#include<conio.h>
struct student
{
int no;
char name[10];
int fee;
};
void display(int,char[],int);
void main()
{
struct student s;
clrscr();
printf("enter student number,name and fee\n");
scanf("%d%s%d",&s.no,&s.name,&s.fee);
display(s.no,s.name,s.fee);
getch();
}
void display(int no,char name[10],int fee)
{
printf("student details are\n");
printf("%d%s%d",no,name,fee);
}
2) Passing the entire structure:
When we pass the entire structure as function argument, we use call by method, i.e. a copy of each
member of the structure is made.
Example:
#include<stdio.h>
#include<conio.h>
struct student
{
int no;
char name[10];
int fee;
};
void display(struct student);
6
void main()
{
struct student s;
clrscr();
printf("enter student number,name and fee\n");
scanf("%d%s%d",&s.no,&s.name,&s.fee);
display(s);
getch();
}
void display(struct student s)
{
printf("student details are\n");
printf("%d%s%d",s.no,s.name,s.fee);
}

3) Passing the address of the structure:

The address location of structure variable is passed to function while passing it by reference. If
structure is passed by reference, change made in structure variable in function definition reflects in
original structure variable in the calling function.
Example:
#include<stdio.h>
#include<conio.h>
struct student
{
int no;
char name[10];
int fee;
};
void display(struct student *);
void main()
{
struct student s;
clrscr();
printf("enter student number,name and fee\n");
scanf("%d%s%d",&s.no,&s.name,&s.fee);
display(&s);
7
getch();
}
void display(struct student *s)
{
printf("student details are\n");
printf("%d%s%d",s->no,s->name,s->fee);
}
Q) Self-referential structures:
Self-referential structures are those structures that contain a reference to data of its same type.
A self-referential structure contains a pointer to a data that is of the same type as that of structures.

Q) Explain unions:

A union is a special data type available in C that allows to store different data types in the same
memory location.
Declaring a union:
A union is declared in the same way as a structure.
The syntax of union declaration is
union union_name
{
type element 1;
type element 2;
……………..
type element n;
};

Here union is the keyword, union_name is the user defined name for union, and type specifies any
valid data type. Element1,2…n are the members of union.

Declaring a union vaiable:


union union_name variable_name;

Accessing members of an union


The member of unions can be accessed in similar manner as that structure by using dot(.) operator.
#include <stdio.h>
#include <conio.h>
union item
{
int a;
8
float b;
char ch;
};
int main( )
{
union item it;
it.a = 12;
it.b = 20.2;
it.ch='z';
clrscr();
printf("%d\n",it.a);
printf("%f\n",it.b);
printf("%c\n",it.ch);
getch();
return 0;
}
Q) Difference between union and structure:
structure union
Keyword struct defines a structure. Keyword union defines a union

struct s Union u
{ {
int val; int val;
char cptr[10]; char cptr[10];
}s; }s;

The amount of memory required to store a unions, the amount of memory required is always
structure variable is the sum of the size of all the equal to that required by its largest member.
members.
In structure, each member have their own In union, one block is used by all the member of
memory space the union.
individual member can be accessed at a time Only one member can be accessed at a time.

9
Q) enumerated data types:
 Enumerated data type is user defined data type.
 The enumerated data type offers us a way of inventing our own data type.
 An enumeration consists of a set of named integers. We assign symbolic names to integer
constants.
 To define enumerated data types we use keyword “enum”.
 Syntax:
enum enum_name
{
enumerator 1;
enumerator 2;
enumerator 3;
.
.
enumerator n;
};
Here enum is the keyword,enumerator1,2 ….n representing the integer constants 0,1,…n;
 We can declare enum variable by
enum enum_name variable name;
Example:#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum week today;
today=wednesday;
printf("%d day",today+1);
getch();
return 0;
}
Example2:
#include< stdio.h>
void main()
{
int i;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
clrscr();
for(i=JAN;i<=DEC;i++)
printf("\n%d",i);}
10

You might also like