|
|
|
Structure
|
|
A Structure is a collection of variables under a single name. These variables can be of different types, and each has a name
which is used to select it from the Structure. A Structure is a convenient way of grouping several pieces of related information
together.
A Structure can be defined as a new named type, thus extending the number of available types. It can use other Structures,
arrays or pointers as some of its members, though this can get complicated unless you are careful.
|
|
Basic Operations of Structure
|
|
A Structure type is usually defined near to the start of a File using a statement.
Which defines and names a new type, allowing its use throughout the program.
Structure usually occur just after the #define and #include statements in a file.
|
struct
{
char name[30];
char course[50];
int age;
int year;
} student;
|
This defines a new type student variables of type student can be declared as follows.
Notice how similar this is to declaring an int or float. The variable name is st_rec, it has members called name, course, age and year.
Each member of a Structure can be used just like a normal variable, but its name will be a bit longer. To return to the
examples above, member name of Structure st_rec will behave just like a normal array of char, however we refer to it by the
name
Here the dot is an operator which selects a member from a Structure. Where we have a pointer to a Structure we could dereference the pointer and then use dot as a member selector. This
method is a little clumsy to type. Since selecting a member from a Structure pointer happens frequently, it has its own
operator -> which acts as follows. Assume that st_ptr is a pointer to a Structure of type student We would refer to the name
member as
Arrays can also be Structure member. Element of Structure can be used like other ordinary variable.
Structure can also used as array of objects for example.
|
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
char course[50];
int age;
int year;
} ;
void main()
{
int i;
struct student st_rec[5];
clrscr();
for(i=0;i<5;i++)
{
printf("Enter %d th Structure data",i);
printf("\n Enter name");
scanf("%s",st_rec[i].name);
printf("Enter Course");
scanf("%s",st_rec[i].course);
printf(" Enter age");
scanf("%d",&st_rec[i].age);
printf("\n Enter year");
scanf("%d",&st_rec[i].year);
}
printf("Students Recors");
for(i=0;i<5;i++)
{
printf("\n name");
printf("%s",st_rec[i].name);
printf(" Course");
printf("%s",st_rec[i].course);
printf(" age");
printf("%d",st_rec[i].age);
printf(" year");
printf("%d",st_rec[i].year);
}
}
|
|
|
Structures as Function Arguments
|
|
A Structure can be passed as a function argument just like any other variable. This raises a few practical issues. Where we wish to modify the value of members of the Structure, we must pass a pointer to that Structure. This is just like passing a pointer to an int type argument whose value we wish to change.
#include<stdio.h>
#include<conio.h>
struct complex
{
float real;
float imag;
};
void main()
{
struct complex c1,c2,c3;
struct complex sum(struct complex,struct complex);
clrscr();
printf("Enter First Complex Number");
printf("\n Enter Real Part");
scanf("%f",&c1.real);
printf("Enter imaginary part");
scanf("%f",&c1.imag);
printf("Enter Second Complex Number");
printf("\n Enter Real Part");
scanf("%f",&c2.real);
printf("Enter imaginary part");
scanf("%f",&c2.imag);
c3=sum(c1,c2);
printf("%f",c3.real);
printf("%f",c3.imag);
}
struct complex sum( struct complex a,struct complex b)
{
struct complex c;
c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return c;
}
|
If we are only interested in one member of a Structure, it is probably simpler to just pass that member. This will make for a
simpler function, which is easier to re-use. Of course if we wish to change the value of that member, we should pass a
pointer to it.
As we have seen, a Structure is a good way of storing related data together. It is also a good way of representing certain
types of information. Complex numbers in mathematics inhabit a two dimensional plane (stretching in real and imaginary
directions). These could easily be represented here by
|
|
Union
|
A union is a variable which may hold (at different times) objects of different sizes and types. That is a union hold only one member at a time. C uses the union statement to create unions, for example.
|
union number
{
short shortnumber;
long longnumber;
double floatnumber;
} anumber |
defines a union called number and an instance of it called
a number. number is a union tag and acts in the same way as a tag for a Structure.
All operation on union like that of Structure.
Difference between Structure and union is Structure allocate storage space for
all the members where as union allow only one member at a time.
Application of union is when we need only one member of a Structure for a
particular application that time we can use union.
|
|
Back
|
|
Next
|
|
|