A structure is a collection of variables of different data types grouped together under a single name. A structure declaration defines the format of the structure, while a structure variable allocates memory for it. Structures allow grouping of related data and can be used within other structures or as elements of an array. Pointers to structures can be used to access member variables using the -> operator. Structures can be passed as arguments to functions to organize related data.
1. Structure:
A structure is a collection of one or more variables of different data
types.grouped together under a single name.
Declaration:
The declaration of structure is:
struct struct_name
{
type variable1;
type variable2;
----------------
}
Where “struct” is the keyword to create a structure variables tag-name
identifies the particular structure and its type.
The declaration has not declared any variables .It simply describes a format
called “template “ to represent information as shown
struct book
{
char title[20];
char name[15];
int pages;
float price;
2. };
The structure declaration is enclosed with in a pair of curly braces.
The fields are called structure elements or members
Eg:
struct stu
{
char name[20];
int rno;
}
struct stu s={“jhon”,43};
Here the name john is assigned to s.name and 43 to s.rno.
The period() sign is used to accesss the structure members.
The link between member and a variable is established using the member
operator . Which is known as dot or period operator.
Eg:s.sname
struct stu s={“aa”,2};
struct stu s1={“bb”,3};
struct s2={“cc”,4};
WRITE A PROGRAM ILLUSTRATING to accept the day month and year and
display it by using structures
void main()
{
struct date
3. {
int day,month,year;
};
struct date d;
printf(“enter day,month,year”);
scanf(“%d%d%d”,&d.day,&d.month,&d.year);
printf(“day=%d,month=%d,year=%d”,d.day,d.month,d.year);
getch();
}
Output: 01 02 2008
Day 1 month 02 year 2008
WRITE A PROGRAM ILLUSTRATING to initialize the members of a structure
student as roll number,name,weight display the content
void main ()
{
int no;
int height, weight;
char name [20];
};
structure student s={3,167,65,”aa”};
printf(“%d%d%d%s”,s.rno,s.height,s.weight,s.name};
getch();
}
Output:
4. 3 167 65 aa
Structure with in a structure:
We can take any data type for declaring structure members like int, float
,char etc.
In the same way we can also take object of one structure as member in
another structure.
Thus,structure with in structure can be used to create complExamle data
applications.
The syntax of the structure within the structure is
struct time
{
int second;
int minute;
int hour;
};
struct t
{
int num;
struct time st;
struct time ct;
};
5. WRITE A PROGRAM ILLUSTRATING structure with in a structure
Array of structures
As we know array is a collection of similar data types.In the same way we
can also define array of structures.
In such type of array every element is of structure type array of structure
can be declared as
struct time
{
int second;
int minute;
int hour;
}t[3];
In this t[3] is an array of 3 elemrnts containing 3 objects or time structure .
Each element of t[3] has structure of time with 3 members that are second
,minute and hour.
Examle program
Array within structures:
C permits the use of arrays as structure members We can use single or multi
dimensional arrays of type int or float.
Examle: struct marks
{
int number;
6. flaot subject[3];
}
student[2];
Here the member subject contains 3 elements
subject[0],subject[1],subject[2];
These elements can be accessed using appropriate subscripts.
Example:
Pointer to structure:
Pointer is a variable that holds the address of another data variable. The
variable may be of any data type i.e int,float or double. Starting address of
the member variable can be accessed by using structure pointers.
struct book
{
char name[25];
char author[25];
int pages;
};
struct book *ptr;
*ptr is a pointer to structure book.The syntax for using pointer with member
is as given
Ptr->name
Structure and fuctions:
User defined datatypes:
There are two user defined datatypes
7. 1.Enumerated datatype
2. Typedef
Enum:
Enum is a keyword .It is used for declaring enumeration types.
- The programmer can create his own datatype and define what values
the variables of these datatypes can hold.
- This enumeration datatype helps in reading the program.
EG: enum month(jan,feb,mar,april,may}
This statement creates a user defined datatype the keyword enum is
followed by the tagname month .enumerators jan,feb,…. And so on are
Identifiers Jan refer to 0,feb to 1 and so on.
The identifiers are not to be enclosed within quotation marks.
-Integer constants are not permitted.
Eg
Type def:
C provides the type def keyword that allows you to specify a new name
for a datatype already provided in the C programming language.
So the typedef keyword to specify an identifier for an Examleisting
datatype.
The declaration for the typedef keyword is:
Typedef datatype new name
This datatype represents the Examleisting datatype for which you want to
specify an identifier and new name refer to that identifier.
Examle: typedef int num;
8. WRITE A PROGRAM ILLUSTRATING for typedef
int main(void)
{
typedef int num;
num a,b,add;
a=10;
b=20;
add=0;
add=a+b;
clrscr();
printf(“the sum of %d and %d is %d”,a,b,add);
getch();
return 0;
}