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;
2. float price;
};
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 dotor period operator.
Eg:s.sname
struct stu s={“aa”,2};
struct stu s1={“bb”,3};
struct s2={“cc”,4};
WAP to accept the day month and year and display it by using structures
void main()
3. {
struct date
{
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
WAP 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”};
4. printf(“%d%d%d%s”,s.rno,s.height,s.weight,s.name};
getch();
}
Output:
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 complex data applications.
The syntax of the structure within the structure is
struct time
{
int second;
int minute;
int hour;
};
struct t
{
int num;
5. struct time st;
struct time ct;
};
WAP 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.
Ex prg
Array within structures:
6. C permits the use of arrays as structure members We can use single or multi
dimensional arrays of type int or float.
Ex: struct marks
{
int number;
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.
Eg
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 byusing structure pointers.
struct book
{
char name[25];
char author[25];
int pages;
};
struct book*ptr;
7. *ptr is a pointer to structure book.Thesyntax for using pointer with member is as
given
Ptr->name
Structure and fuctions:
User defined datatypes:
There are two user defined datatypes
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
8. 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 existing datatype.
The declaration for the typedef keyword is:
Typedef datatype new name
This datatype represents the existing datatype for which you want to specify an
identifier and new name refer to that identifier.
Ex: typedef int num;
WAP 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);