Saturday 31 August 2013

Structures and Unions Introduction



STRUCTURES & UNIONS

Data in the array is of the same composition in native a fan as type is concerned. In real life we need to have different data types for ex. To maintain the employee information. We should have information such as name, age, qualification, salary etc. here to maintain the information of employee dissimilar data types required. Name & qualification are char data type age is integer, & salary is float. All these data types cannot be expressed in a single away. One may think to declare different always for each data type. Hence, always cannot be useful her for tackling such mixed data types, a special feature is provided by C, known as Structure.
Structure is a collection of heterogeneous type of data i.e. different types of data. The various individual components, in a structure can be accessed is processed separately. A structure is a collection of variables referenced under a name, providing a convenient means of keeping related information. A structure declaration forms a template that may be used to create structure objects.

Difference between structure is arrays :-
                 Structure                                                     Arrays
1. Collection of heterogeneous types     1. Collection of homogeneous types of of data i.e. different                                                               types of data. data i.e. same types of data
2. A structure element component         2. An away it is referred by its position of structure has a                                                                 unique name. i.e. index.
The point on which array & structures can be similar is that both array & structure must be definite number of components.

Features of Structures:-
To copy elements of one array to another array of same data type elements are copied one by one. It is not possible to copy elements at a time. Where as in structure it is possible to copy the contents of all structure elements of different data types to another structure var of its type using assignment (=) operator. It is possible because structure elements are stored in successive memory locations. Nesting of structures is possible.
It is also possible to create structure pointers. We can also create a pointer. Pointing to structure elements.
For this we require ' -> ' operator.

Declaration of structure :-
Struct Struct_type
{
type val 1; // members of structures
type val 2;
};

After defining structure we can create variables as given Struct struct_type v1, v2, v3.
The declaration defines the structure but this process does not allocate memory. The memory allocation takes only when var.r declared
Example:-
Struct book
{
Char book [30];
int pages;
float price;
};
Strict book bk1, bk2;
Initialization:-
Struct book bk1= {“siri”, 500, 38500};
All the members of structure are related to variable bk1 structure – var. member i.e. bk1.book
The period (.) sign is used to access the struct members.
WAP to define & initialize a structure & its variables
main ( )
{
Strict book
{
char book [30];
int pages;
float price;
};
struct book bk1 = { “c++”, 300, 285};
clrscr ( );
printf (“ \n Book name : %s “ , bk1.book);
printf (“\n No of pages :%d “,bk1.pages);
printf (“\n book price :%f “,bk1.price);
}
Out put:
Book name : C ++
No of pages : 300
Book Price : 285

Structure within structure (nested):-
We can take any data type for declaring structure members like int, float, char etc. In the same way wee can also take object of one structure as member in another structure.
The syntax as follows:-
struct s1
{
Type var1;
Type var2;
};
struct s2
{
Type v3;
Strict s1 v4;
Strict s2 v5;
};
struct s2 p;

=> WAP to enter the records of no of students & then display them using nested structure
# include <stdio.n> # define max 300
Struct data
{
int day;
int month;
int year;
};
Struct student
{
char name [30];
long int rollno;
struct data dob;
};
struct student bio [max];
void main ()
{
int i, n;
printf (“ How many student data required:”);
scanf (“%d”,&n);
for(i=0;i<=n-1; i++)
{
printf (“Record. No : % d\n”, i+1);
printf (“Student name :”); sf(“ %s”,bio[i].name);
printf (“Roll no :”) ; sf (“ %d “, bio [i].rollno);
pf (“date of birth”); pf (“\n Day”);
printf (“%d”, &bio [i].dob.day);
printf (“ month:”); sf (“%d”, &bio[i].dob.month)
printf (“year:”) ; sf (“% d”, & bio[i].dob.year).
}
printf (“ The entered contents are :”);
for (i=0;i <=n-1;i++)
{ printf (“\n Record No : % d”, i+1).
printf (“\n Student name : % s”, bio [i].name);
printf (“\n Roll no : % d “, bio [i] . rollno);
printf (“\n Date of birth “);
printf (“\n day : % d”,bio[i].dob.day);
printf (“\n month : % d”, bio [i].dob. month);
printf (“\n year : % d”, bio [i].dob.year);
}
}

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved