Structure:: General Format of Structure
Structure:: General Format of Structure
Structure:
If we want to represent a collection of data items of different data types using a single
name. We can not use an array; structure is a method of packaging data of different types.
It is analogous to record.
Structure sometimes referred to as agreements collection of related variables under one
name . Structure may contain variables of different data types. i.e. collection of related
variables of different data types under one name called structure.
Structure is commonly used to define records to be stored in files. Pointers and structures
facilitate the formation of more complex data structures such as link list, queues, stacks
and trees.
struct
{
data_type member1;
data_type member1;
...............
};
} tag_name;
30 byte
Street
40 byte
City
State
Id
20 byte
5 byte
2 byte
Zip
4 byte
Fig: Info structure in memory.
A structure personal which contain person name, date of joining and salary. Using
this structure, write a program to read this information for one person from
keyboard and print the same on the screen.
#include<stdio.h>
struct personal
{
char name[20];
int day;
char month[15];
int year;
float salary;
};
int main()
{
struct personal person;
printf("\n Input data");
scanf("%s %d %s %d %f",
person.name,
&person.day,
person.month,
&person.year,
&person.salary);
printf("\t%s %d %s %d %f",
person.name,
person.day,
person.month,
person.year,
person.salary);
return 0;
}
Structure assignment:
The information contained in one structure can be assigned to another structure of
the same type using a single assignment statement. We do not need to assign the
value of each member separately.
//Structure Assignment
#include<stdio.h>
struct
{
int a;
int b;
}x,y;
int main()
{
clrscr();
x.a=10;
x.b=20;
y=x; //assign one structure to another
printf("\n%d",y.a);
printf("\n%d",x.b);
return 0;
}
Func1 (&value.x);
Func2 &(value.y);
Func3 (&value.z);
Func4 (value.s);
Func5 (&value.s[2]);
int main()
{
value.x='M';
value.y=45;
value.z=50.50;
printf("\nEnter String: ");
scanf("%s",value.str);
Function(value.x,value.y,value.z,value.str,value.str[3]);
Return 0;
}
Passing Entire Structures to Functions:
Union:
A union is a derived data type like structure with members that share the same storage
space. The members of a union can be of any data types, it can handle only one member
at a time.
General Format:
union tag_name
{
data_type member1;
data_type member1;
. . . . . . . . . . .
};
E.g.
union item
{
int m;
float x;
char c;
}code;
1000
1001
1002
1003
C
m
x
Fig: Sharing a storage space locating by union members.
To access a union member, we can use the same syntax that we can use in structure.
e.g.
code.m
code.x
code.m
Note: Remember that when we are accessing the members or fields of union, we can
access the member whose value is currently stored. The number of bytes used to store
union must be at least enough to hold the largest member.
//Simle example of union
#include<stdio.h>
union number
{
int x;
double y;
};
int main()
{
union number value;
value.x=100;
printf("\n\t x=%d \n\t y=%f \n",value.x,value.y);
value.y=200.50;
printf("\n\t x=%d \n\t y=%f \n",value.x,value.y);
return 0;
}