Structure
is a mechanism that provides a means to aggregate variables of different type.
Thus a single structure might contain integer elements, floating-point
elements, and character elements. Pointers, arrays, and other structures can
also be included as elements within a structure. The individual structure
elements are referred to as members.
Unlike structure, the array is a
mechanism that provides a means to aggregate variables of similar type. That
is, all elements in the array are of same data type.
Defining/Declaring
a Structure
Structure declarations are
somewhat more complicated than array declarations, since a structure must be
defined in terms of its individual members. Its general form is given below:
struct structurename {
member 1;
member
2;
..........
memver
m;
};
Here, struct
is a keywork, structurename is a name of the structure, and member
1, member 2,…, member
m are individual members.
These individual members can be
ordinary variables, arrays, pointers, or other structures. The member names
within a particular structure must be distinct from one another, though a
member name can be the same as the name of a variable that is defined outside
the structure. A storage class (e.g. auto, extern, static, or register) cannot
be assigned to an individual member, and individual members cannot be
initialized within a structure.
Once the composition of the
structure has been defined, individual structure-type variables can be declared
as follows:
storage-class struct structurename variable 1, variable 2, …, variable
n;
Here, storage-class is optional, struct
is a required keyword, structurename is the name that
appeared in the structure declaration, and variable 1, variable 2,…,
variable n are structure variables of type structurename.
For example, a
typical structure declaration is shown below. This structure is named account (i.e., the structurename
account). It contains four members: an integer quantity (acct_no), a single character (acct_type),
an 80-element character array (name[80]),
and a floating-point quantity (balance).
struct account {
int
acct_no;
char
acct_type;
char
name[80];
float
balance;
};
We can now declare the structure variables oldcustomer and
newcustomer as follows:
struct account
oldcustomer, newcustomer;
It is also possible to combine
the declaration of the structure composition with that of the structure
variables, as shown below:
storage-class struct
structurename {
member 1;
member 2;
..........
memver m;
} variable 1, variable 2, …, variable n;
In this situation, storage-class and structurename are optional. For example,
struct account {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
} oldcustomer,
newcustomer;
OR
struct {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
} oldcustomer, newcustomer;
A structure variable may be defined as a member of another
structure. A structure that contains other structures as members is called nested structure. In such situations,
the declaration of embedded structure must appear before the declaration of the
outer structure. For example,
struct date {
int
month;
int
day;
int
year;
};
struct account {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
struct
date lastpayment;
};
struct account oldcustomer, newcustomer;
The members of a structure variable can be assigned initial
values in much the same manner as the elements of an array. The initial values
must appear in the order in which they will be assigned to their corresponding
structure members, enclosed in braces and separated by commas. The general form
is:
storage-class struct structurename variable = {value 1, value 2, …,
value m};
Here, value 1 refers to the
value of the first member, value 2 refers to the value of the second member,
and so on. For example,
struct date {
int
month;
int
day;
int
year;
};
struct account {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
struct
date lastpayment;
};
account customer = {1234, 'C', "Nawaraj", 1234.7, {5, 24, 90}};
It is also possible to define an
array of structures; i.e., an array in which each element is a structure. For
example,
struct date {
int
month;
int
day;
int year;
};
struct account {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
struct
date lastpayment;
};
struct account customer[100];
An array of structures can be assigned initial values just
as any other array. For example,
struct account
customer[] = {{1234, 'C', "Nawaraj", 1234.7, {5, 24, 90}}, {4567,
'C', "Ram", 1034.5, {12, 24, 99}}};
Processing
a Structure
The members of a structure are processed (accessed) as
separate entities using period (.) that separates variable name from the member
name. Its general form is:
variable.member
Here, variable refers to the name of a structure-type
variable, and member refers to the name of a member within the structure. For
example,
struct date {
int
month;
int day;
int
year;
};
struct account {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
struct
date lastpayment;
};
struct account customer;
Now, you can access acct_no
using customer.acct_no, acct_type using customer.acct_type, name
using customer.name, balance using customer.balance, month
using customer.lastpayment.month, day using customer.lastpayment.day, and year
using customer.lastpayment.year.
For arrays of structures, we write
array[index].member
For example,
struct account customer[100];
Now, you can access acct_no
using customer[i].acct_no, acct_type using customer[i].acct_type, name
using customer[i].name, balance using customer[i].balance, month
using customer[i].lastpayment.month, day using customer[i].lastpayment.day, and year using customer[i].lastpayment.year.
A
Complete Example
Program to display
customer information of customers having balance greater than 1000 using previous
structures.
#include<stdio.h>
#include<conio.h>
#define N 100
void main() {
struct date {
int month;
int
day;
int
year;
};
struct account {
int acct_no;
char
acct_type;
char
name[10];
float
balance;
struct
date lastpayment;
};
struct account customer[N];
int
n,i;
clrscr();
printf("How
many customers:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter
data for customer %d:\n",i+1);
printf("Enter account
number:");
scanf("%d",&customer[i].acct_no);
printf("Enter
account type:");
scanf("
%c",&customer[i].acct_type);
printf("Enter name:");
scanf("
%[^\n]",customer[i].name);
printf("Enter balance:");
scanf("%f",&customer[i].balance);
printf("Enter month:");
scanf("%d",&customer[i].lastpayment.month);
printf("Enter day:");
scanf("%d",&customer[i].lastpayment.day);
printf("Enter year:");
scanf("%d",&customer[i].lastpayment.year);
}
printf("\nCustomers
having balance greater than 1000:\n");
for(i=0;i<n;i++)
{
if(customer[i].balance >
1000) {
printf("Account
number: %d\n",customer[i].acct_no);
printf("Account
type: %c\n",customer[i].acct_type);
printf("Name:
%s\n",customer[i].name);
printf("Balance:
%f\n",customer[i].balance);
printf("Month:
%d\n",customer[i].lastpayment.month);
printf("Month:
%d\n",customer[i].lastpayment.day);
printf("Month:
%d\n",customer[i].lastpayment.year);
}
}
getch();
}
Structures and Pointers
We can use
pointers with structure variables in the same manner as any other variables.
For example,
struct account {
int
acct_no;
char
acct_type;
char
name[10];
float
balance;
};
struct
account customer, *pc;
pc = &customer;
In this example, customer is a structure variable of type account, and pc is a
pointer to customer.
An individual
structure member can be accessed in terms of its corresponding pointer variable
by using -> operator. For
example,
scanf("%d",
&pc->acct_no);
Note: A structure
may include one or more pointers as its members. For example,
struct account {
int
*acct_no;
char
*acct_type;
char
*name;
float
*balance;
};
Passing
Structures to Functions
We can pass
structure variables to a function by passing a structure type pointer as an
argument. A structure passed in this manner will be passed by reference rather than by value. Hence, if any of the
structure members are altered within the function, the alteration will be
recognized outside the function. For example,
#include<stdio.h>
#include<conio.h>
struct
account {
int acct_no;
char
acct_type;
char
name[10];
float
balance;
};
void
main() {
struct account customer;
void
update(struct account *pt);
clrscr();
printf("Enter
account number:");
scanf("%d",&customer.acct_no);
printf("Enter account type:");
scanf("
%c",&customer.acct_type);
printf("Enter
name:");
scanf("
%[^\n]",customer.name);
printf("Enter
balance:");
scanf("%f",&customer.balance);
update(&customer);
printf("Account
number: %d\n",customer.acct_no);
printf("Account
type: %c\n",customer.acct_type);
printf("Name:
%s\n",customer.name);
printf("Balance:
%f\n",customer.balance);
getch();
}
void
update(struct account *pt) {
pt->balance = pt->balance *
1.07;
}
Note: Newer
version of C also permit an entire structure to be transferred directly to a
function as an argument and returned directly from a function via a return
statement. Here, the transfer is by value rather than by reference. Therefore,
if any of the structure members are altered within the function, the alteration
will not be recognized outside the function.
Self-referential
Structure
It is sometimes desirable to include within a structure one
member that is a pointer to the parent structure type. Hence, a structure which
contains a reference to itself is called self-referential structure. In general
terms, this can be expressed as:
struct
structurename {
member 1;
member 2;
…….
struct structurename *name;
};
For example,
struct
list_element {
int item[40];
struct list_element *next;
}
This is a structure of type
list_element. The structure contains two members: a 40-element integer array
called item, and a pointer to a structure of the same type (i.e., a pointer to
a structure of type list_element), called next. Therefore this is a
self-referential structure.
Unions
Unions, like structures, contain members whose individual data
types may differ from one another. However, the members within a union all
share the same storage area within the computer’s memory, whereas each member
within a structure is assigned its own unique storage area. Thus, unions are
used to conserve memory. They are useful for applications involving multiple
members, where values need not be assigned to all of the members at any one
time. In general terms, the composition of a union may be defined as
union
unionname {
member 1;
member 2;
…..
member m;
};
Here, union is a required keyword and the other terms have the same
meaning as in a structure definition. Individual union variables can then be
declared as:
storage-class union unionname variable 1, variable 2, …, variable n;
Here, storage-class is optional,
union is a required keyword, unionname is the name that appeared in the union
definition, and variable 1, variable 2, …,
variable n are union variable of type unionname. For example,
union
id {
char
color[12];
int
size;
};
union id shirt, blouse;
Here, we have two union variables,
shirt and blouse, of type id. Each variable can represent either a 12-character
string (color) or an integer quantity (size) at any one time.
Two declarations
may be combined, just as we did with structures. Thus, we can write it as
follows:
storage-class union
unionname {
member 1;
member 2;
…..
member m;
} variable 1, variable
2, …, variable n;