Structure And Union

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.

General format of structure:
struct tag_name
data_type member1;
data_type member2;

data_type member1;
data_type member1;


} tag_name;

E.g. struct Info
char name[30];
char street[40];
char city[20];
char state[5];
int Id;
long zip;

30 byte


40 byte


20 byte
5 byte
2 byte

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.
struct personal
char name[20];
int day;
char month[15];
int year;
float salary;

person. &person. struct Info student1={ 60. main() { struct Info { int weight. struct Info student2={ 62. . . } . float height }.  The order of values enclosed in braces must match the order of members in the structure definition. . .salary). . .185. . .Structure And Union int main() { struct personal person. printf("\t%s %d %s %d %f".month. . person. E. The uninitialized members should be only at the end of the list. printf("\n Input data"). .salary).g. . person.  The uninitialized members will be assigned default values as follows: o Zero for integers and floating point numbers. person. . person.50}.  It is permitted to have a partial initialization. &person. .year. . scanf("%s %d %s %d %f". We can initialize only the first few members and leave the remaining . Structure Initialization: Rules:  We can not initialize individual members inside the structure template.50}. . The general form for accessing a member of a structure is: Object_name. . .‟ operator (usually called the dot operator).year. } Accessing Structure Members: Individual members of a structure are accessed through the use of the „. o „###BOT_TEXT###‟ for characters and strings. return 0. &person. person. person.

b=20.char s[]. Func4 ( y. } Func1 (&value. Func4 (value.s[2]).y). float z.h> struct func { char x.y. Func5 (value. We do not need to assign the value of each member separately.x). char str[10]. //assign one structure to another printf("\n%d". x. Func3 (value.h> struct { int a.s. //Structure Assignment #include<stdio.x.y). // passing structure member to function #include<stdio.x.a).c).char c) { printf("\n\t%c\n\t%d\n\t%f\n\t%s\n\t%c". }value. int y. }value. . This structure member is passed to the function in the following way: Func1 (value.a=10.z. void Function(char x. x. It is irrelevant that the value is obtained from a member of a structure.s). printf("\n%d". y=x. Struct func { char x.float z. float z. int main() { clrscr(). int b. Func2 &(value.z).s[2]).b).s). int y. we are passing the value of that member to the function.y. char str[10].y. return 0.Structure And Union Structure assignment: The information contained in one structure can be assigned to another structure of the same type using a single assignment statement. Func3 (&value. } Structure And Function Passing Structure Members to Functions: When we pass a member of a structure to a function. Func2 (value.x). }x.z). Func5 (&value.

arg.z.str.x='M'. scanf("%s".z=50.value.y.str). int y. value.value.value. This means that any changes made to the content of the parameter inside the function do not affect the structure passed as the argument. value. printf("\nEnter String: "). Function(value.Structure And Union int main() { value.x.str). } int main() { value. When using a structure as a parameter. value.50. scanf("%s".y=45.x. return 0. value.z.h> struct func { char x. remember that the type of the argument must match the type of the parameter. char str[10].value.value. Return 0.z=50.y. Function(value).str[3]).arg.arg.y=45. void Function(struct func arg) { printf("\n\t%c\n\t%d\n\t%f\n\t%s".50. printf("\nEnter String: "). // passing Entire structure to function #include<stdio.arg.value. the entire structure is passed using the normal call-by-value method. } . } Passing Entire Structures to Functions: When a structure is used as an argument to a function. float z.str). }value.x='M'.

}code. value.Structure And Union Union: A union is a derived data type like structure with members that share the same storage space.value. .x.h> union number { int x. .value. float x. printf("\n\t x=%d \n\t y=%f \n".g. . . e. . it can handle only one member at a time. int main() { union number value. .y).x. . code. .y).x code.m Note: Remember that when we are accessing the members or fields of union.y=200. we can use the same syntax that we can use in structure. we can access the member whose value is currently stored. } . The members of a union can be of any data types.m code.g. . }. data_type member1. . double y. .value. value. }. 1000 1001 1002 1003 C m x Fig: Sharing a storage space locating by union members.value. char c. printf("\n\t x=%d \n\t y=%f \n". The number of bytes used to store union must be at least enough to hold the largest member.50. return 0. E.x=100. //Simle example of union #include<stdio. To access a union member. General Format: union tag_name { data_type member1. union item { int m.