Structure:: General Format of Structure

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Structure And Union

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.

General format of structure:


struct tag_name
{
data_type member1;
data_type member2;
..............

struct
{
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;
}
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;
};

Structure And Union

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;
}

Accessing Structure Members:


Individual members of a structure are accessed through the use of the .
operator (usually called the dot operator). The general form for accessing a
member of a structure is:
Object_name.member_name;
Structure Initialization:
Rules:

We can not initialize individual members inside the structure template.


The order of values enclosed in braces must match the order of members in
the structure definition.
It is permitted to have a partial initialization. We can initialize only the first
few members and leave the remaining blank. The uninitialized members
should be only at the end of the list.
The uninitialized members will be assigned default values as follows:
o Zero for integers and floating point numbers.
o \0 for characters and strings.
E.g. main()
{
struct Info
{
int weight;
float height
};
struct Info student1={ 60,180.50};
struct Info student2={ 62,185.50};
. . . . . . . . . . . . . . . . . . .
}

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. 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;
}

Structure And Function


Passing Structure Members to Functions:

When we pass a member of a structure to a function, we are passing the


value of that member to the function. It is irrelevant that the value is obtained from
a member of a structure.
Struct func
{
char x;
int y;
float z;
char str[10];
}value;

This structure member is passed to the function in the following way:


Func1 (value.x);
Func2 (value.y);
Func3 (value.z);
Func4 (value.s);
Func5 (value.s[2]);
// passing structure member to function
#include<stdio.h>
struct func
{
char x;
int y;
float z;
char str[10];
}value;
void Function(char x,int y,float z,char s[],char c)
{
printf("\n\t%c\n\t%d\n\t%f\n\t%s\n\t%c",x,y,z,s,c);
}

Func1 (&value.x);
Func2 &(value.y);
Func3 (&value.z);
Func4 (value.s);
Func5 (&value.s[2]);

Structure And Union

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:

When a structure is used as an argument to a function, the entire structure is


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

Structure And Union

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;
}

You might also like